我是新手,我在登录时遇到了一些麻烦。 这是我的jQuery:
jQuery(document).ready(function () {
$.ajax({
type: 'POST',
url: 'login.php',
crossDomain: true,
data: 'uname=TEST',
dataType: 'json',
async: false,
success: function (response){
alert ("success");
if (response.success) {
alert("you're logged in");
}else {
alert("Your login failed");
}
},
error: function(error){
alert('Could not connect to the database' + error);
}
});
});
问题是它总是进入错误回调,我已经尝试自己运行PHP文件并且它有效(如果我在那里硬编码用户名)。
我不知道为什么总是出错,有什么帮助?
这是PHP:
<?php
header("access-control-allow-origin: *");
header('Content-type: application/json');
include 'conection.php';
mysql_query('SET CHARACTER SET utf8');
$uname = $_POST['user'];
$sql = "SELECT IdUser FROM user WHERE IdUser = '$uname'";
$result = mysql_query($sql);
$num_row = mysql_num_rows($sql);
$row = mysql_fetch_array($result);
//echo $sql . " " . mysql_affected_rows();
if (mysql_affected_rows() == 1) {
$response['success'] = true;
}else{
$response['success'] = false;
}
echo json_encode($response);
?>
答案 0 :(得分:0)
您传递给AJAX电话的参数是&#39; uname&#39;,但您在php中寻找的参数是&#39;用户&#39; - 改变其中一个以匹配另一个。
答案 1 :(得分:0)
尝试更新如下:
$.ajax({
type: 'POST',
url: 'login.php',
//crossDomain: true,
data: {"user":"TEST"}, // updated the uname to user which is @ php side
dataType: 'json',
//async: false,
我认为你可以从你的ajax中取出crossDomain:true
和async:false
。