从我的表单中,我可以成功地发送正确的数据并接收正确的响应,但我一直纠结于如何解码PHP变量的响应,以便在会话中操作和存储它。
我收到这样的回复:
request.done(function (response, textStatus, jqXHR){
// log a message to the console
console.log(response);
request.fail(function (jqXHR, textStatus, errorThrown){
// log the error to the console
console.error(
"The following error occured: "+
textStatus, errorThrown
);
我的回答如下:
error: 1
error_msg: "Incorrect"
success: 0
tag: "login"
error: 0
success: 1
tag: "login"
user: Object
profileimageurl: "test"
uid: "1"
userEmail: "email@domain.com"
userFirstName: "Test"
userLastName: "User"
如果成功,我将如何获得失败的错误消息和用户ID?
编辑:添加了请求代码
$('#loginSubmit').click(function () {
//start the ajax
var f = $("#loginForm");
request = $.ajax({
//this is the php file that processes the data
url: "app/index.php",
type: "POST",
data: f.serialize(),
dataType:"json",
cache: false
});
request.done(function (response, textStatus, jqXHR){
console.log(response);
request.fail(function (jqXHR, textStatus, errorThrown){
// log the error to the console
console.error(
"The following error occured: "+
textStatus, errorThrown
);
alert("fail")
});
我尝试过$json_decode(响应),但没有定义?很确定我不能在javascript中使用普通的旧PHP?我基本上想要的是:
request.done(function (response, textStatus, jqXHR){
myphpvar = response[user][id]
}
尝试使用parseJSON()函数对其进行解码,如下所示:
var obj=jQuery.parseJSON(响应);alert(obj.uid);