如何检索jquery $ .ajax对象的responseJSON属性 [英] How retrieve responseJSON property of a jquery $.ajax object

查看:366
本文介绍了如何检索jquery $ .ajax对象的responseJSON属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个javascript:

I have this javascript:

$ajax = $.ajax({
    type: 'GET',
    url: 'DBConnect.php',
    data: '',
    dataType: 'json', 
    success: function(data) {},
    error:function (xhr, ajaxOptions, thrownError) {
        dir(thrownError);
        dir(xhr);
        dir(ajaxOptions);
    }
});
console.dir($ajax);
console.dir($ajax.responseJSON);

console.dir($ ajax)显示它有一个名为responseJSON的属性,但当我尝试访问时它与$ ajax.responseJSON一起返回undefined:

console.dir($ajax) shows it has a property named responseJSON, but when I try to access it with $ajax.responseJSON it returns undefined:

推荐答案

嗯,当然它是未定义的,因为当你在最后一行运行 console 时你的代码,响应尚未来自服务器。

Well, of course it's undefined, because at the moment when you run console at last lines of your code, response hasn't yet came from the server.

$。ajax 返回promise,您可以使用它来附加 done() fail()回调,您可以在其中使用您看到的所有属性。而且你实际上使用了回调错误 success ,这就是你可以运行依赖于数据的代码和其他函数的地方在回复中。

$.ajax returns promise, which you can use to attach done() and fail() callbacks, where you can use all the properties that you see. And you have actually used callback error and success, and that's where you can run code and other functions that rely on data in the response.

这篇关于如何检索jquery $ .ajax对象的responseJSON属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆