jQuery.get()
S2C Home « Ajax « jQuery.get()
Asynchronous HTTP (Ajax) load request.
Description
The jQuery.get()
Ajax method, allows us to load data from the server, using a HTTP GET request.
Shorthand version $.get()
- Browsers operate the 'same origin policy', which means a request can not successfully retrieve data from a different domain, subdomain, or protocol. jQuery 'Ajax' requests are subject to this security restriction, the exceptions being requests made with a datatype of 'script' or 'jsonp'.
- The
jQuery.get()
method returns ajqXHR
object which is a superset of the browser's nativeXMLHttpRequest
object. ThejqXHR
object implements thePromise
interface, giving it all the properties, methods, and behavior of a Promise. See the lesson on theDeferred
object for details of this. For standardization with the theDeferred
object, thejqXHR
object also providesdone()
,fail()
andalways()
methods. These methods take a function argument that is called when thejQuery.get()
request terminates and the function receives the same arguments as the same-namedsuccess()
,error()
andcomplete()
setting callbacks respectively. This allows assignment of multiple callbacks for a single request as well as assigning callbacks after the request has completed. For completed requests, the callback is fired immediately.- It should be noted that the
success()
,error()
andcomplete()
callbacks of thejqXHR
object are deprecated from use on request completion from jQuery 1.8 and removed in jQuery 3.0. This is for completion processing only and the Ajax settings will still have these values. Best practice is to usedone()
,fail()
andalways()
methods on request completion to future proof code or use the newthen()
andcatch()
methods for Promises/A+ compliance.
- It should be noted that the
- A
jqXHR
object will expose the following properties and methods for backward compatibility with theXMLHttpRequest
object:
jqXHR Object | Description |
---|---|
Methods | |
abort() | Cancels the currently executing request. |
getAllResponseHeaders() | Returns a string containing the names and value of all response headers. |
getResponseHeader(name) | Returns the value of the specified response headers. |
.overrideMimeType() | Used in the beforeSend() callback function, to modify the response content-type header. |
setRequestHeader(name, value) | Set a request header using the specified name and value. |
Properties | |
readyState | An integer indicating the current state of the request. |
responseText | Underlying request responded with text. |
responseXML | Underlying request responded with xml. |
status | Response status code returned from the server. |
statusText | Status text message returned by the response. |
Syntax
Signature | Description |
---|---|
jQuery.get( url [,data] | Load data from the server, using a HTTP GET request. |
jQuery.get( settings ) | Load data from the server using a HTTP GET request, passing some request settings. |
Parameters
Parameter | Description | Type |
---|---|---|
url | A string containing the URL to send the request to. | String |
data | An object or string sent to the server with the request.
|
String orPlainObject |
function(data, textStatus, jqXHR) | A callback function executed when the request succeeds. | Function |
dataType | The type of data expected from the server.
|
String |
settings | Default settings can be set globally by using the jQuery.ajaxSetup() method but the settings as described in jQuery.ajax( settings ) can be useful for individual Ajax requests. |
Various dependant upon settings parameter. |
Return
A jqXHR
object.
jQuery.get( url [, data] [, function(data,
textStatus, jqXHR)] [, dataType] )
ExamplesTop
textStatus, jqXHR)] [, dataType] )
Load data from the server, using a HTTP GET request.
This is a shorthand Ajax function, which is equivalent to using the jQuery.ajax()
method as follows:
$.ajax({
url: url,
dataType: dataType,
data: data,
success: callback
});
In the example below when we press the left button the first time we use the jQuery.get()
method in its simplest form just using a url and output some messages dependant upon request success.
When we press the right button the first time we use the jQuery.get()
method with a url and a success handler and output some messages dependant upon request success.
$(function(){
$('#btn1').one('click', function(){
$.get( "../js/get1.js" )
.done(function() { $('#div1').append('Request succeeded! <br>'); })
.fail(function() { $('#div1').append('Request failed! <br>'); })
.always(function() { $('#div1').append('Request ended! <br><br>');
});
});
$('#btn2').one('click', function(){
$.get( "../js/get1.js", function(data, textStatus, jqXHR) {
$('#div1').append('The request was a success! <br>');
})
.done(function() { $('#div1').append('Request succeeded! <br>'); })
.fail(function() { $('#div1').append('Request failed! <br>'); })
.always(function() { $('#div1').append('Request ended! <br><br>');
});
});
});
/*
* The code for the external Javascript file called from $.ajax() (url: "../js/get1.js")
* is shown below.
*/
$(function(){
var someText = 'Passing some text. ';
$('#div1').append('Message from get1.js: ' + someText + '<br>');
});
div1. Some initial text.
jQuery.get( settings )
ExamplesTop
Load data from the server using a HTTP GET request, passing some request settings.
This is a shorthand Ajax function, which is equivalent to using the jQuery.ajax()
method as follows:
$.ajax({
type: 'GET',
url: url,
dataType: dataType,
error: callback
success: callback
});
In the example below when we press the left button the first time we use the jQuery.get()
using a url, dataType and error and success parameters and output a fail messages as the url doesn't exist.
In the example below when we press the right button the first time we use the jQuery.get()
using a url, dataType and error and success parameters and output a success message and the contents of the retrieved url.
$(function(){
$('#btn10').one('click', function(){
$.get({
url: "../js/get22.js",
dataType: "script",
error: function() {
$('#div2').append('The request failed! <br><br>');
},
success: function() {
$('#div2').append('The request was a sucesss! <br><br>');
}
});
});
$('#btn11').one('click', function(){
$.get({
url: "../js/get2.js",
dataType: "script",
error: function() {
$('#div2').append('The request failed! <br><br>');
},
success: function() {
$('#div2').append('The request was a sucesss! <br><br>');
}
});
});
/*
* The code for the external Javascript file called from $.ajax() (url: "/../js/get2.js")
* is shown below.
*/
$(function(){
var someText = 'Passing some text. ';
$('#div2').append('Message from get2.js: ' + someText + '<br>');
});
div2. Some initial text.
Related Tutorials
jQuery Advanced Tutorials - Lesson 9 - Ajax Shorthand Methods