简体   繁体   English

Json没有将值发布到PHP

[英]Json not posting values to PHP

i'm trying out a simple login form through phonegap, the code is herewith. 我正在尝试通过phonegap的简单登录表单,代码是随此。 my problem is that JSON is not passing the values from my phonegap page to the PHP service. 我的问题是JSON无法将值从我的phonegap页面传递到PHP服务。 Any help is appreciated Here is my script: 感谢您的帮助,这是我的脚本:

 $('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;
});

My PHP page is as follows 我的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);
     ?> 
$('form').submit(function(){
    //var postData = $(this).serialize();
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    $.ajax({
        type: 'POST',
         data : {username:username, password:password},

        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;
});

you can not get that data straight forward using $_POST varriable.to accept json you need to read from stdin input 您无法使用$ _POST varriable.or来直接获取该数据以接受需要从stdin输入读取的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);

?> ?>

I have tested with following javascript code: 我已经使用以下JavaScript代码进行了测试:

<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>

and with folling php code: 并使用以下php代码:

<?php
$request = file_get_contents('php://input');
$reqRarray = json_decode($request,true);
var_dump($reqRarray);
?>

and get that output on ajax: 并在ajax上获得该输出:

array (size=2)  'username' => string 'username' (length=8)  'password' => string 'password' (length=8)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM