Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a synchronous request with jQuery?

Why don't return that function the responseText?

function LoadBookmarksAsXml()
{
  return $.ajax(
  {
    type: 'GET',
    async: false,
    url:  'http://www.google.com/bookmarks/?output=xml&num=10000'
  }).responseText;
}

(It works if I define a success-callback-function and set async to true!) Thanks in advance!!

Edit: Don't worry about the cross-domain call; user603003 says (in a comment on a now-deleted answer) that this is in a Chrome extension where cross-domain requests are allowed.

The solution if someone wants to do the same:

return $.ajax(
{
  type: 'GET',
  async: false,
  url:  'http://www.google.com/bookmarks/?output=xml&num=10000',
});

(You will get a XMLHTTPRequest object.)

like image 401
ComFreek Avatar asked Feb 04 '11 10:02

ComFreek


People also ask

Can jQuery send asynchronous request?

ajax() Function. The jQuery $. ajax() function is used to perform an asynchronous HTTP request. It was added to the library a long time ago, existing since version 1.0.

How can make synchronous AJAX call in jQuery?

you can call ajax by button click event and remove the form on submit and use form submit function on success event.

Can AJAX requests be made synchronous?

AJAX can access the server both synchronously and asynchronously: Synchronously, in which the script stops and waits for the server to send back a reply before continuing. Asynchronously, in which the script allows the page to continue to be processed and handles the reply if and when it arrives.

Is jQuery synchronous?

Synchronous AJAX call is made when async setting of jQuery AJAX function is set to false while Asynchronous AJAX call is made when async setting of jQuery AJAX function is set to true. Default value of the async setting of jQuery AJAX function is true.


1 Answers

I'm not immediately seeing why it's not returning it, but I'd still use a success callback:

function LoadBookmarksAsXml()
{
  var result;
  $.ajax(
  {
    type: 'GET',
    async: false,
    url:  'http://www.google.com/bookmarks/?output=xml&num=10000',
    success: function(data) {
        result = data;
    }
  });
  return result;
}

Even though $.ajax returns an XMLHttpRequest object (in 1.4 or earlier) or a jqXHR object (in 1.5+), I'd still prefer using a success function and an error function for clarity. Also, different versions of jQuery give you different values for responseText on error (at least on Chrome; 1.4.4 returns an empty string, 1.5.0 returns undefined).


If there's any way you can avoid it, avoid it. Synchronous requests completely lock up the UI of most browsers (not just your page's UI, every page in every tab that browser is managing). Since ajax requests can take a second or two (or five, or ten), this makes for a very unpleasant user experience. Nearly all the time, you can avoid it by refactoring your function so it accepts a callback to use to supply the result:

function LoadBookmarksAsXml(callback)
{
  $.ajax(
  {
    type: 'GET',
    url:  'http://www.google.com/bookmarks/?output=xml&num=10000',
    success: function(data) {
        callback(data);
    },
    error: function() {
        callback(null);
    }
  });
}

Off-topic: I'll be surprised if the request works at all, though, because on the face of it (unless you work for Google), that request will fail because of the Same Origin Policy. Various ways to get around the SOP:

  • JSONP
  • CORS (but it requires browser support and that www.google.com allow the request from your origin)
  • Using YQL as a proxy
like image 104
T.J. Crowder Avatar answered Oct 06 '22 00:10

T.J. Crowder