我做了一个使用ajax发送表单到一个PHP文件,它应该返回结果..however在几个浏览器我得到的错误,而在Firefox中它似乎像魅力一样工作。 JS:ajax + php + json =登录
$.ajax({
type: "POST",
url: "http://www.xxxxxx.net/login.php",
data: dataString,
dataType: "json",
success: function(json) {
if(json.jresult==false){
alert(json.jerror);
}
else{
if(json.identification==0 || json.identification==1){
// register: do some stuff
}
else if(json.identification==2){
// login: do some stuff
}
else{
return(false);
}
}
},
error: function(xhr, textStatus, errorThrown) {
alert('error:'+errorThrown+',status:'+textStatus+',xhr:'+xhr);
},
complete: function(jqXHR, textStatus) {
alert(textStatus);
}
});
错误,我从使用Safari,Opera和RockMelt的Ajax调用获得:
@complete功能: textStatus =错误 @error功能:错误:,状态:错误,XHR:[对象物体]
PHP:
session_cache_limiter('nocache');
header('Expires: '.gmdate('r',0));
header('Content-type: application/json');
// set response array
$aDataR = array();
$aDataR['jresult'] = false;
$aDataR['identification'] = -1;
if(!isset($_POST['iID'])){
}
else{
if($_POST['iID']==0){
/* REGISTER */
}
elseif($_POST['iID']==1){
/* LOST PASS */
}
elseif($_POST['iID']==2){
/* LOGIN */
$aDataR['identification'] = 2;
if(false==($mResponse = $cChallenges->CheckLogin($_POST['sUser'],$_POST['sPass']))){
$aDataR['jerror'] = "No such user/password combination";
$cChallenges->ProbeLogin($_POST['sUser'],$_SERVER['REMOTE_ADDR']);
}
else{
$aDataR['jresult'] = true;
$aDataR['jsuccess'] = "Thanks for logging in";
}
}
}
echo htmlspecialchars(json_encode($aDataR), ENT_NOQUOTES);
忽略所有不重要的代码。 希望任何人都可以帮助我解决这个问题,提前致谢。
你的ajax调用的实际结果是什么?在调试器控制台中查看它。什么是xhr.responseText? – Bergi
值为空。 – JustaN00b
这听起来像一个PHP错误,json_encode应该总是打印一个字符串。尝试调试您的PHP应用程序。 – Bergi