jQuery getJSON() Guide: Load JSON Data
The getjson() method in jQuery is used to load data in JSON format from a server. It is used to send an HTTP GET request and retrieve JSON data from the server. This method makes it easy to fetch data from the server and then display or manipulate it on the webpage.
The basic syntax for using the getjson() method is as follows:
$.getJSON(url, data, success);
- URL: The address where the data needs to be loaded from.
- Data: Optional parameter that is sent to the server.
- Success: an optional parameter, the callback function to be executed upon a successful request.
For example, the following code demonstrates how to use the getjson() method to fetch JSON data from the server and display this data on the page.
$.getJSON('data.json', function(data) {
$.each(data, function(key, value) {
$('body').append('<p>' + key + ': ' + value + '</p>');
});
});
The code above will load JSON data from a file named data.json, then iterate over the data using the $.each() method and display each key-value pair on the page.