-6

Possible Duplicate:
Difference between $.ajax() and $.get() and $.load()

What's the difference between $.get() and $.ajax

The code I've been given uses calls like this:

$.get(href)
   .success(function (content) {
        $('#content')

and:

$.ajax({
    cache: false,
    url: href + params.param,
    dataType: 'html'
})

Can someone explain what the difference is between these. Is there any advantage to using one over the other?

Community
  • 1
  • 1
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

4 Answers4

3

Can someone explain what the difference is between these.

See the documentation for get:

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

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

Is there any advantage to using one over the other?

One is shorter. One is more flexible.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

.get(), .getJSON(), .getScript(), .post(), and .load() are all helper methods that call .ajax().

Think of them as shortcuts to get the same result with less code.

Phil
  • 10,948
  • 17
  • 69
  • 101
0

I believe that under the covers they both do the same thing. The main difference is that .get is a simpler interface which makes it easier to use but is a lot less flexible in terms of what you can do with it. .ajax on the other hand has all the functionality in there which means you can do whatever you want but is a little more complex.

As a rule of thumb use .get if you can and when that doesn't seem to do quite what you want move on to useing .ajax instead.

Chris
  • 27,210
  • 6
  • 71
  • 92
0

You can read about this here:

$.ajax http://api.jquery.com/jQuery.ajax
$.get http://api.jquery.com/jQuery.get

all the documentation is available on the jQuery website

Alex
  • 1,630
  • 1
  • 20
  • 31