如何检索jquery $ .ajax对象的responseJSON属性

编程入门 行业动态 更新时间:2024-10-23 03:19:14
本文介绍了如何检索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属性

本文发布于:2023-11-26 21:01:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   jquery   ajax   responseJSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!