我有这个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属性
发布评论