带有async false的jQuery ajax调用不能工作

时间:2022-10-16 20:15:01

Here I have pasted my code, I want to return the response of $.ajax as response of function a(). But before the result comes up of ajax call, it is returning the empty f. please help on this

这里我粘贴了代码,我想返回$的响应。ajax作为函数a()的响应。但是在ajax调用的结果出现之前,它返回的是空的f

a = function()
{
        var f = '';
    $.ajax({
          url: 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=immaulikvora&count=1&page=1&include_entities=1&callback=?',
          dataType: 'json',
          async: false,
          success: function(data) {
            f = data;
          }
        });    
    return f;
};


var lid = a();

alert(lid);

3 个解决方案

#1


8  

I guess you are using jQuery 1.8+

我猜你用的是jQuery 1.8+

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.ajax/

Please read the fine print.

请阅读小字。

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated;

在jQuery 1.8中,使用async: false与jqXHR ($.Deferred)被弃用;

you must use the complete/success/error callbacks.

您必须使用完整/成功/错误回调。

try

试一试

http://jsfiddle.net/UgrLE/

http://jsfiddle.net/UgrLE/

#2


10  

Please assign the ajax to jqXHR object and reading the responseText will help you.

请将ajax分配给jqXHR对象,阅读responseText将帮助您。

 var jqXHR=$.ajax({
      url: 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=immaulikvora&count=1&page=1&include_entities=1&callback=?',
      dataType: 'json',
      async: false
    });    

jqXHR.responseText // This will give you the result

#3


1  

Unless you work for twitter, I would assume it is failing because "Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation."

除非您在twitter上工作,否则我认为它失败了,因为“跨域请求和数据类型:”jsonp“请求不支持同步操作”。

Deprecated != removed and even if it did I take the deprecation is in the context of the returned jqXHR which are not being used here.

弃用!=删除,即使我使用了弃用,在返回的jqXHR的上下文中也没有使用。

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.ajax/

It looks like @Murali provides a work-around, but the above is important to point out for those having issue with same-domain requests.

看起来@Murali提供了一种工作,但是上面的内容对于那些有相同域请求的人来说是很重要的。

#1


8  

I guess you are using jQuery 1.8+

我猜你用的是jQuery 1.8+

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.ajax/

Please read the fine print.

请阅读小字。

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated;

在jQuery 1.8中,使用async: false与jqXHR ($.Deferred)被弃用;

you must use the complete/success/error callbacks.

您必须使用完整/成功/错误回调。

try

试一试

http://jsfiddle.net/UgrLE/

http://jsfiddle.net/UgrLE/

#2


10  

Please assign the ajax to jqXHR object and reading the responseText will help you.

请将ajax分配给jqXHR对象,阅读responseText将帮助您。

 var jqXHR=$.ajax({
      url: 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=immaulikvora&count=1&page=1&include_entities=1&callback=?',
      dataType: 'json',
      async: false
    });    

jqXHR.responseText // This will give you the result

#3


1  

Unless you work for twitter, I would assume it is failing because "Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation."

除非您在twitter上工作,否则我认为它失败了,因为“跨域请求和数据类型:”jsonp“请求不支持同步操作”。

Deprecated != removed and even if it did I take the deprecation is in the context of the returned jqXHR which are not being used here.

弃用!=删除,即使我使用了弃用,在返回的jqXHR的上下文中也没有使用。

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.ajax/

It looks like @Murali provides a work-around, but the above is important to point out for those having issue with same-domain requests.

看起来@Murali提供了一种工作,但是上面的内容对于那些有相同域请求的人来说是很重要的。