jQuery get() method
The get() method is an inbuilt function in jQuery. It loads data from the server using the HTTP GET request. It is used for making a simple GET request. It returns XMLHttpRequest object.
Syntax
Parameter Values
This method includes four parameter values in which one is mandatory, and others are optional. We must have to include the URL parameter to the get() method. The parameter values of the get() method are defined in the following table.
Parameter | Values |
---|---|
URL | It is the URL to which the request is sent. It is a mandatory parameter. |
data | It is an optional parameter. It is data sent to the server along with the request. It can be a plain object or a string. |
function(data, status, xhr) | This is also an optional parameter. It is a callback function that executes on the successful server request. It also has three parameters that are data, status, and xhr in which data contains the resulting data from the request, status represents the request status like “success”, “error”, etc., and the xhr contains the XMLHttpRequest object. |
dataType | This is also an optional parameter that defines the type of data we are expecting from the server. The type can be “text”, “json”, “jsonp”, “html”, “script”, and “XML”. |
Now, let’s see a simple example of using the get() method.
Example
In this example, we are making a get request by using the get() method. We are using two parameters of the get() method that are URL and a callback function.
The URL parameter is set to the value test.html. We are using two parameters of the callback function that are data and status in which the first parameter holds the data of the requested page, and the second parameter holds the request status.
In the output, we can see the data load from the server, and the status of the request is success.
test.html
Example2.html
Output
After the execution of the above code, the output will be –
After clicking the given button, the output will be –