How can I execute a synchronous query using jQuery?

Why not return this responseText function?

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

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

Change Do not worry about cross-domain calling; user603003 says (in the comment on the remote response) that this is in the Chrome extension where cross-domain requests are allowed.

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 an XMLHTTPRequest object.)

+4
source share
5 answers

I do not immediately see why it does not return it, but I still use the 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 if $.ajax returns an XMLHttpRequest object (at 1.4 or earlier) or a jqXHR object (at 1.5+), I would prefer to use the success function and the error function for clarity. In addition, different versions of jQuery give you different values ​​for responseText on error (at least in Chrome: 1.4.4 returns an empty string, 1.5.0 returns undefined ).


If exists , you can avoid it , avoid it. Synchronous requests completely block the user interface of most browsers (and not just the page user interface, each page on each tab controlled by the browser). Since ajax requests can take a second or two (or five or ten), this creates a very nasty user interface. Almost all the time, you can avoid this by refactoring your function so that it accepts a callback to use 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); } }); } 

Not in the subject . I will be surprised if the request works at all, because at first glance (if you do not work at Google) this request will fail due to the same origin policy . Different ways to get around SOP:

+10
source

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

You still need to determine a successful callback, I think, for example. one parameter sets a local variable that you can return.

Standard Disclaimer: Synchronous requests are usually discouraged because they can freeze the current page.

+1
source

Waiting for a response from the function is not asynchronous, the ajax call will have the answer, when it is done, you must take care of the answer, and then, defining the callbacks for the successful event.

You can break your code into at least two parts. The first part is before the ajax call, the second part is after success, and puts everything you want to do with the requested data in the success callback. Asynchronous requests work this way.

0
source

This is a very bad idea. Javascript will block throughout the HTTP request, which means that nothing in the user interface thread will work until the ajax call returns. Use callback.

0
source

By design, asynchronous requests cannot provide a Text response from blue ;-) You need to set the callback function and decide how you will handle the responseText.

0
source

Source: https://habr.com/ru/post/1338410/


All Articles