The getJSON()
method in jQuery fetches JSON data from a server using the HTTP GET method.
$.getJSON(url, data, function());
This method accepts the following parameters.
url
: This is the URL on which the request is to be made.data
: It specifies some additional data that can be sent to the server. It is optional.function()
: This is a callback function that is invoked when the request is completed.This method returns an object of the XMLHttpRequest
type.
Let’s look at an example of the getJSON()
method in the code snippet below:
In the HTML tab:
jQuery
script.Fetch Data
.content
.In the JavaScript tab:
click()
method on the Fetch Data
button.getJSON()
method to get data from this URL.content
.If the request is successful, the callback function will be invoked, and the data will be appended in the div content
.