Difference between $.ajax() and $.get() and $.load()
Solution 1:
$.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/
Solution 2:
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()
.
Solution 3:
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.
Solution 4:
Very basic but
-
$.load()
: Load a piece of html into a container DOM. -
$.get()
: Use this if you want to make a GET call and play extensively with the response. -
$.post()
: Use this if you want to make a POST call and don’t want to load the response to some container DOM. -
$.ajax()
: Use this if you need to do something when XHR fails, or you need to specify ajax options (e.g. cache: true) on the fly.
Solution 5:
Important note : jQuery.load() method can do not only GET but also POST requests, if data parameter is supplied (see: http://api.jquery.com/load/)
data Type: PlainObject or String A plain object or string that is sent to the server with the request.
Request Method The POST method is used if data is provided as an object; otherwise, GET is assumed.
Example: pass arrays of data to the server (POST request)
$( "#objectID" ).load( "test.php", { "choices[]": [ "Jon", "Susan" ] } );