2

私のIndex.htmlファイルコードは次のとおりです。

<!DOCTYPE html> 
<html>
<head>
<meta charset="utf-8">
<title>Mindcorpus - Placement</title>
<link href="style.css" rel="stylesheet" type="text/css"/>
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery-mobile.js" type="text/javascript"></script>
<script src="/cordova.js" type="text/javascript"></script>
<script>
$(function()
{
$('#frm').submit(function()
{
    var username = $('#textinput').val();
    var username = $.trim(username);
    var password = $('#passwordinput').val();
    var password = $.trim(password);
    if(username=='')
    {
        $('.error').html('Please enter username');
        return false;
    }
    else if(password =='')
    {
        $('.error').html('Please enter password');
        return false;
    }
    else
    {
        var user = $('[name=username]').val();
        var pass = $('[name=password]').val();
        $.ajax({
        type: 'POST',
        url: 'http://localhost/mc-new/admin/mobile1/process.php',
        data: { username: user, password: pass},       
        success: function(data){
            alert(data.success);
        },
        error: function(){
            alert('error!');
        }
    });
 return false;
    }
});
});
</script>
</head> 
<body> 
<div data-role="page" id="page">
<div data-role="header">
    <h1><img src="images/logo.png" /></h1>
</div>
<div data-role="content">
    <div class="error"></div>
    <form action="" method="post" id="frm">
      <div data-role="fieldcontain">
        <input type="text" name="username" placeholder="Username" id="textinput" value=""  />
    </div>
      <div data-role="fieldcontain">
        <input type="password" name="password" placeholder="******" id="passwordinput" value=""       />
    </div>
      <button data-icon="arrow-r" type="submit">Submit</button>
    </form>
</div>
<div data-role="footer">
    <h4 style="font-weight:normal">Powered By: Mind Processors</h4>
</div>
 </div>
 </body>
</html>

そして、私の process.php ファイルは次のとおりです。

<?php
if(isset($_POST['username']) || isset($_POST['password'])) 
{
    $data['success'] = 'Done';
    echo json_encode($data);
}
?>

ブラウザで localhost を使用すると、この関数は正しく機能します。しかし、Dreamweaver を使用してこのサイトを構築およびエミュレートすると、. それは常にエラーを警告します。Ajax がエミュレーターで機能していません。リクエストはプロセス ファイルに渡されません。この問題を解決してください。

4

1 に答える 1

0

予想される戻りデータ型がJSONであり、クロスドメインを有効にする必要があることを ajax で指定する必要があります。

以下の ajax のコードを試してください。

$.ajax({
    type: 'POST',
    url: 'http://localhost/mc-new/admin/mobile1/process.php',
    crossDomain: true,
    data: { username: user, password: pass},
    dataType: 'json',
    success: function(data){
        alert(data.success);
    },
    error: function(){
        alert('error!');
}

また、php ファイルに以下を追加します。

<?php

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Max-Age: 1000');
header('Content-Type: application/json');

if(isset($_POST['username']) || isset($_POST['password'])) 
{
    $data['success'] = 'Done';
    echo json_encode($data);
}
?>

これが、この質問に陥り、答えを探している人に役立つことを願っています.

于 2015-04-20T04:52:59.667 に答える