Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax call with async false is not working

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

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);
like image 318
Maulik Vora Avatar asked Oct 17 '12 08:10

Maulik Vora


1 Answers

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

 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
like image 195
Murali Murugesan Avatar answered Nov 07 '22 03:11

Murali Murugesan