简体   繁体   English

获取$ .ajax jquery成功内返回的数据

[英]get data returned inside success of $.ajax jquery

I have a question here. 我在这里有一个问题。 Let's say I have some Ajax with jQuery like so: 假设我在jQuery中有一些Ajax,例如:

var jqXHR = $.ajax({
       type: 'POST', 
       success: function(data) {
             if(data != true)
             {
                  return false;
             }
       }
});

I know that $.ajax returns a jqXHR object. 我知道$ .ajax返回一个jqXHR对象。 My question is the following: 我的问题如下:

Is it possible to get the returned value of the success function of my $.ajax call using that jqXHR object? 是否可以使用该jqXHR对象获取$ .ajax调用的成功函数的返回值? If so, how do I do that? 如果是这样,我该怎么做? If that's not possible using the jqXHR object, is there any way that I can get access to the returned value of my success function WITHOUT SETTING async: false in $.ajax? 如果使用jqXHR对象无法做到这一点,我有什么方法可以访问我的成功函数的返回值,而无需在$ .ajax中设置async:false?

Any help please? 有什么帮助吗?

It is not possible without setting async to false . 没有将async设置为false这是不可能的。 I would suggest not to set it to false because it stops the page completely until the server response comes. 我建议不要将其设置为false,因为它会在服务器响应到来之前完全停止页面。 Sometimes it even hangs the browser if the connection is slow or server takes time to respond due to heave operation. 有时,如果连接速度慢或服务器由于沉重的操作而需要时间来响应,它甚至会挂起浏览器。

You can execute your code in the success handler of ajax which you are planning to do it outside. 您可以在计划在外部进行的ajax成功处理程序中执行代码。

It is possible - in a way - returned jqXHR is also a deferred object so you could do 从某种意义上说,返回的jqXHR也是一个延迟对象,因此您可以

jqXHR.then(function(data) { ... });

the only other way to get access to the data that i know of apart of the ajax callbacks, the cool thing is that you can use it multiple times after the ajax request was sent and it will always return the data you've received from the server. 访问ajax回调中我知道的数据的另一种方法是,很酷的事情是,在ajax请求发送之后,您可以多次使用它,并且它将始终返回从ajax请求中接收到的数据服务器。

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

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