简体   繁体   English

jQuery.ajax调用中php的json响应出现问题

[英]trouble with json response from php in jQuery.ajax call

I am using $.ajax to get a JSON response from a php script. 我正在使用$ .ajax从php脚本获取JSON响应。 if i log the data variable from the $.ajax success function it outputs a properly formatted JSON object, however when I try to access properties of the data var it's undefined. 如果我从$ .ajax成功函数记录数据变量,它将输出格式正确的JSON对象,但是当我尝试访问数据变量的属性时,它是未定义的。 here is the php the is being sent back: 这是被发送回的PHP:

echo json_encode(array("status"=>true, "success"=>"Login Success", "message"=>"You have been logged in successfully."));

and here is my ajax call: 这是我的ajax电话:

$.ajax({
        type: "POST",
        data: {
            login: true,
            username: $('#login-username').val(),
            password: $('#login-password').val()
        },
        async: false,
        url: "./php/client-login.php",
        success: function (data) {
            console.log(data.status);
            if (data.status) {
                console.log(data.success);
                displayModal(data.success, data.message, "./js/login-modal-code.js");
            } else if (!data.status) {

                displayModal(data.error, data.message, "./js/login-modal-code.js");
            }
        },
        error: function (jqXHR, status, responseText) {
            console.log(status);
        }
});

if i add the dataType: "json" option to the $.ajax call I get a parse error and if i try to do $.parseJSON(data); 如果我将dataType: "json"选项添加到$ .ajax调用中,则会出现解析错误,并且如果我尝试执行$.parseJSON(data); to access the data in the data var I get and unexpected token error. 访问我得到的数据变量中的数据和意外的令牌错误。 I'm not really sure what I'm doing wrong, I've used this setup before and it always has worked before but for some reason it isn't now. 我不太确定自己在做什么错,我以前曾经使用过此设置,但以前一直可以使用,但是由于某种原因,现在还没有。 anyone see where i've gone wrong? 有人看到我错了吗?

EDIT: forgot to mention here is the response from the php script: 编辑:忘记提及这里是来自php脚本的响应:

{"status":true,"success":"Login Success","message":"You have been logged in successfully."}

EDIT 2: Here is a screen of my console. 编辑2:这是我的控制台的屏幕。 the top .length call is the json that was logged from console.log(data) and the bottom one is from the response in chrome dev tools network tab for the response from the php script. 最上面的.length调用是从console.log(data)记录的json,最下面的是从chrome dev tools网络选项卡中的响应记录的php脚本的响应。 they line up perfectly yet the second is showing a length of 93, how can i fix this? 他们排列完美,但第二个显示长度为93,我该如何解决? 控制台映像

I was reading on jQuery Docs and found that "dataType: jsonp" does not work with sync request, and you're making this kind of request since you have async: false . 我在阅读jQuery Docs时发现,“ dataType:jsonp”不适用于同步请求,由于您具有async: false ,因此您正在发出这种请求。 Turning it on may solve your problem. 打开它可能会解决您的问题。

Also, give a look at the docs. 另外,看看文档。

Good luck! 祝好运!

So I figured out a workaround for this problem, first I did JSON.stringify on the data followed by JSON.parse and lastly $.parseJSON() to get a javascript object to work with. 所以我想出了一个解决此问题的方法,首先我先对数据执行JSON.stringify,然后对JSON.parse进行数据处理,最后对$ .parseJSON()进行处理,以获取可使用的javascript对象。 Not sure why there are 2 invisible characters being added between when it leaves the php script and reaches the $.ajax call so if anyone knows why let me know 不知道为什么在离开php脚本并到达$ .ajax调用之间添加了2个不可见字符,所以如果有人知道为什么让我知道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM