24

I was wondering whether I should use asynchronous calls on my website or not. I know to specify this explicitly I need to use

$.ajax

however initially I tried using $.get and although the server had to return lots of information, my browser didn't stuck and I could navigate without any problems.

I searched a little bit online about it, however I'm still not 100% certain about the difference between the two.

If $.get is asynchronous then what's the point of $.ajax? And if it's not, then again seeing how I had no navigation problems with $.get, what's the point of using $.ajax?

thanks in advance

ksm001
  • 3,772
  • 10
  • 36
  • 57
  • 2
    Have you read the [documentation](http://api.jquery.com/jQuery.get/)? – Dennis Dec 02 '12 at 17:36
  • 1
    yes however since I couldn't see the point of creating a shorthand about something that is already short, I asked here to make sure. – ksm001 Dec 02 '12 at 17:43

4 Answers4

37

Yes, $.get is asynchronous. From the documentation:

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

...and as that doesn't have the async option, async defaults to true. (Note that async will be going away entirely in a future version of jQuery; it will always be true.)

If $.get is asynchronous then what's the point of $.ajax?

$.ajax gives you control over lot more options. $.get is just a shortcut.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

$.get is simply a shorthand for:

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
João Silva
  • 89,303
  • 29
  • 152
  • 158
0

As stated by the jQuery documentation, $.get is a convenience wrapper for the more low-level $.ajax.

Connor Doyle
  • 1,812
  • 14
  • 22
0

$.ajax with GET method, How about this with a promise object?

function showResults(name) { 
        var deferred = $.Deferred, requests = [];

        requests.push($.ajax({url:"/path/to/uri/?name=" + name, type: "GET", async: false}).done(function(d) { 
         //alert or process the results as you wish 
        }));
        $.when.apply(undefined, requests).then(function(d) { console.log(d); /*var d is a promised late return */ deferred.resolve(); }); 
        return deferred.promise();

    }

he returned promise object can also be used with $.when(showResults('benjamin')).done(function() { }); for post modifications (like chart/graph settings etc). totally reusable. You may also put this function in a loop of $.deferred requests like,

function updateResults() { 
             var deferred = $.Deferred, requests = [];
             requests.push($.ajax(url:"/path/to/names/?nameArr=" + jsonArrOfNames, type: "GET", async: false}).done(function(res) {  requests.push(showResults(res[0]));}) );
             $.when.apply($, requests).then(function(d) { console.log(d); /*var d is a promised late return */  deferred.resolve(); }); 
             return deferred.promise();
            }
Arun Panneerselvam
  • 2,263
  • 1
  • 17
  • 24