从我的表单中我可以成功发送正确的数据并收到正确的响应,但我仍然坚持如何解码对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]
}
答案 0 :(得分:0)
尝试使用parseJSON()函数对其进行解码,如下所示:
var obj = jQuery.parseJSON(response); alert(obj.uid);