我通过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);
?>
答案 0 :(得分:1)
$('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;
});
答案 1 :(得分:0)
你无法使用$ _POST varriable.to接受你需要从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);
&GT?;
我已使用以下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>
并使用下载的PHP代码:
<?php
$request = file_get_contents('php://input');
$reqRarray = json_decode($request,true);
var_dump($reqRarray);
?>
并在ajax上获取该输出:
array (size=2) 'username' => string 'username' (length=8) 'password' => string 'password' (length=8)