简体   繁体   中英

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.)

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:

$.ajax never returns the response text, it always returns the XMLHTTPRequest object created to make the Ajax call.

You'll still need to define a success callback I think, eg one setting a local variable which you can then return.

Standard disclaimer: Synchronous requests are a usually discouraged practice because they can freeze the current page.

Waiting for the response of a function is not asyncronous, the ajax call will have a response when it is done, you have to take care of the response then, by defining callbacks for the successful event.

You have ti break up your code to at least two parts. First part is before the ajax call, second part is after the success, and put everything you want to do with the requested data in the success callback. Asyncronous requests work this way.

Doing that is a really bad idea. Javascript will block for the duration of the HTTP request, which is to say nothing else in the UI thread will run until the ajax call returns. Use a callback.

按照设计,异步请求无法提供响应文本;-)您必须设置回调函数并决定如何处理responseText。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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