![](/img/trans.png)
[英]php return to $.ajax in submit login form (jquery ajax does not working when returning with php json_encode)
[英]ajax + php + json = login
我使用ajax进行了登录,将表单发送到php文件,该文件应该返回结果。但是,在一些浏览器中,我遇到了错误,而在Firefox中,它看起来像是一种魅力。 JS:
$.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调用中得到的错误:
@完成功能: textStatus =错误@错误功能:错误:,状态:错误,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数据仅以chrome&firefox格式正确返回,即使如此,如果您将json发送给自己,也可以吗? 请澄清。
可能这是编码问题。 尝试这个
echo htmlspecialchars(json_encode($aDataR), ENT_QUOTES);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.