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:
source share