iman453
iman453

Reputation: 9535

Synchronous ajax calls with jsonp workaround?

I know I can't make synchronous ajax calls with the data type as jsonp and was wondering if there's any workaround for that. I'm trying to learn javascript and was trying to write a function

chrome.omnibox.onInputChanged.addListener(function(text, suggest){
       var baseUrl = "http://sample.com";
       var finalResult = [];
              $.ajax({
                     url : baseUrl,
                     dataType : "jsonp",
                     success: function(result) {
                                     for (var i=0; i<result[1].legnth; i++){
                                          finalResult.push(
                                                 {content : result[1][i], description : result[1][i]}
                                          );
                                     }
                              },
                     async: false
              });           
       suggest(finalResult);
});

I need to provide the suggest() function the results of my ajax call. So I would need the ajax call to be synchronous right? I can't do that because then i run into the same origin policy problem. Any help would be appreciated. Thanks!

Upvotes: 0

Views: 1567

Answers (1)

devang
devang

Reputation: 5516

Invoke the suggest function from your anonymous success function, that way the ajax call can be asynchronous.

 chrome.omnibox.onInputChanged.addListener(function(text, suggest){
      var baseUrl = "http://sample.com";
      var finalResult = [];
      $.ajax({
           url : baseUrl,
           dataType : "jsonp",
           success: function(result) {
                for (var i=0; i<result[1].legnth; i++){
                     finalResult.push(
                          {content : result[1][i], description : result[1][i]}
                     );
                }
                suggest(finalResult);
           },
           async: true
      });             
 });

Upvotes: 0

Related Questions