Difference between $.ajax() and $.get() and $.load()
http://api.jquery.com/jQuery.ajax/
jQuery.ajax()
Description: Perform an asynchronous HTTP (Ajax) request.
The full monty, lets you make any kind of Ajax request.
http://api.jquery.com/jQuery.get/
jQuery.get()
Description: Load data from the server using a HTTP GET request.
Only lets you make HTTP GET requests, requires a little less configuration.
http://api.jquery.com/load/
.load()
Description: Load data from the server and place the returned HTML into the matched element.
Specialized to get data and inject it into an element.
$.ajax()
is the most configurable one, where you get fine grained control over HTTP headers and such. You're also able to get direct access to the XHR-object using this method. Slightly more fine-grained error-handling is also provided. Can therefore be more complicated and often unecessary, but sometimes very useful. You have to deal with the returned data yourself with a callback.
$.get()
is just a shorthand for $.ajax()
but abstracts some of the configurations away, setting reasonable default values for what it hides from you. Returns the data to a callback. It only allows GET-requests so is accompanied by the $.post()
function for similar abstraction, only for POST
.load()
is similar to $.get()
but adds functionality which allows you to define where in the document the returned data is to be inserted. Therefore really only usable when the call only will result in HTML. It is called slightly differently than the other, global, calls, as it is a method tied to a particular jQuery-wrapped DOM element. Therefore, one would do: $('#divWantingContent').load(...)
It should be noted that all $.get()
, $.post()
, .load()
are all just wrappers for $.ajax()
as it's called internally.
More details in the Ajax-documentation of jQuery: http://api.jquery.com/category/ajax/
The methods provide different layers of abstraction.
$.ajax()
gives you full control over the Ajax request. You should use it if the other methods don't fullfil your needs.$.get()
executes an AjaxGET
request. The returned data (which can be any data) will be passed to your callback handler.$(selector).load()
will execute an AjaxGET
request and will set the content of the selected returned data (which should be either text or HTML).
It depends on the situation which method you should use. If you want to do simple stuff, there is no need to bother with $.ajax()
.
E.g. you won't use $.load()
, if the returned data will be JSON which needs to be processed further. Here you would either use $.ajax()
or $.get()
.