Json未将值发布到PHP

Json未将值发布到PHP,php,ajax,json,cordova,Php,Ajax,Json,Cordova,我正在通过phonegap尝试一个简单的登录表单,代码如下。我的问题是JSON没有将值从我的phonegap页面传递到PHP服务。谢谢你的帮助 这是我的剧本: $('form').submit(function(){ //var postData = $(this).serialize(); var username = document.getElementById("username").value; var password = document.getEleme

我正在通过phonegap尝试一个简单的登录表单,代码如下。我的问题是JSON没有将值从我的phonegap页面传递到PHP服务。谢谢你的帮助 这是我的剧本:

 $('form').submit(function(){
    //var postData = $(this).serialize();
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    $.ajax({
        type: 'POST',
        data:JSON.stringify({username:"username",password:"password"}),
        ContentType: "application/json; charset=utf-8",
        crossDomain: true,
        dataType: 'json',
        url: 'http://10.0.2.2:81/comment.php',
        success: function(response){
             alert ("response"); 
                        if (response) { 
                            alert("you're logged in");
                            }
                            else {

                            alert("Your login failed");

                        }

        },
        error: function(){

            alert('There was an error with your login');
        }
    });

    return True;
});
我的PHP页面如下

    <?php 
    header('content-type: application/json');
    header("access-control-allow-origin: *");

    $server = "localhost";
    $username = "root";
    $password = "";
    $database = "comment";

    $con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());

    mysql_select_db($database, $con);

     $username=$_POST["username"]; 
     $password=$_POST["password"]; 
     $sql="SELECT username, password FROM comment WHERE username = '".$username."' AND password = '".$password."'"; 
     $result = mysql_query($sql); 
     if (mysql_num_rows($result) < 1)
     { $response =true; }
    else
    { $response =false; 
    }

    mysql_close($con); 
    echo json_encode($response);
     ?> 

使用$\u POST variable无法直接获取该数据。要接受json,需要从stdin输入读取

<?php 
header('content-type: application/json');
header("access-control-allow-origin: *");

$server = "localhost";
$username = "root";
$password = "";
$database = "comment";

$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());

mysql_select_db($database, $con);
 ////here you need to change
 $request = file_get_contents('php://input');
 $reqRarray = json_decode($request,true);
//////`enter code here`
 $username=$reqRarray["username"]; 
 $password=$reqRarray["password"]; 
 $sql="SELECT username, password FROM comment WHERE username = '".$username."' AND password = '".$password."'"; 
 $result = mysql_query($sql); 
 if (mysql_num_rows($result) < 1)
 { $response =true; }
else
{ $response =false; 
}

mysql_close($con); 
echo json_encode($response);

请不要忘记提及OP代码中的错误以及您对其所做的更改。我担心它将如何(
file\u获得内容('php://input“);
)从
POST
ajax请求读取数据?实际上它将读取POST-json请求字符串。请参阅它发送json请求体,您需要使用文件获取内容(”)php://input'); 获取json请求字符串。您无法使用普通的$\u POST varriableWell获取它,很高兴知道。谢谢但我从未使用过
php://input
,对我来说有点陌生。我总是使用
$\u POST
,而且效果很好。:)实际文件获取内容('php://input'); 用于读取原始post数据,json请求是原始post数据。因此一切正常:)好的,但在再次读取原始json字符串后,我们需要使用
json\u decode()
。最好使用
$\u POST
。相信我,这很管用。无需使用外部函数来转换json和所有内容,因为OP中给出的示例表明ajax将传递单个参数,而不是json。
<?php 
header('content-type: application/json');
header("access-control-allow-origin: *");

$server = "localhost";
$username = "root";
$password = "";
$database = "comment";

$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());

mysql_select_db($database, $con);
 ////here you need to change
 $request = file_get_contents('php://input');
 $reqRarray = json_decode($request,true);
//////`enter code here`
 $username=$reqRarray["username"]; 
 $password=$reqRarray["password"]; 
 $sql="SELECT username, password FROM comment WHERE username = '".$username."' AND password = '".$password."'"; 
 $result = mysql_query($sql); 
 if (mysql_num_rows($result) < 1)
 { $response =true; }
else
{ $response =false; 
}

mysql_close($con); 
echo json_encode($response);
<html>
<head>
    <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
        <script  type='text/javascript'>
         $(document).ready(function(){
            //var postData = $(this).serialize();
            var username = 'test';
            var password = 'meu';
            $.ajax({
                type: 'POST',
                data:JSON.stringify({username:"username",password:"password"}),
                ContentType: "application/json; charset=utf-8",
                crossDomain: true,
                dataType: 'json',
                url: 'ajax.php',
                success: function(response){
                     alert ("response"); 
                                if (response) { 
                                    alert("you're logged in");
                                    }
                                    else {

                                    alert("Your login failed");

                                }
                },
                error: function(){

                    alert('There was an error with your login');
                }
            });

            return true;
         });
        </script>
</head>
<body></body>
<?php
$request = file_get_contents('php://input');
$reqRarray = json_decode($request,true);
var_dump($reqRarray);
?>
array (size=2)  'username' => string 'username' (length=8)  'password' => string 'password' (length=8)