Updated April 17, 2023
Definition of jQuery ajax JSON Response
Basically, Ajax is used to exchange the data with the server and update the current webpage without refreshing and reloading the whole page. By using the jQuery ajax method we can call them or we can say that we can request the different types of text and post such as HTML, XML, and JSON from the remote server as well as it uses the get and post method that is HTTP protocol. So with help of this request and method, we can easily get the response from the current webpage. The JSON is a JavaScript library and it is used to traverse the HTML document.
What is jQuery ajax JSON response?
The $.getJSON() technique is a convenient partner for working with JSON straightforwardly in the event that you don’t need a lot of additional design. Basically, it comes down to the more broad $.ajax() assistant, with the ideal choices being utilized certainly. Basically, jQuery requires JavaScript code to get the response from the server that is the ajax or we can call it DOM, so it will combine all the required files from the server and display the result that we want. The jQuery library contains the accompanying components such as HTML/DOM control, CSS control, HTML occasion techniques, Impacts, and activities, AJAX, and Utilities.
How to Use jQuery ajax JSON response?
Now let’s see how we can use jQuery ajax JSON response for better understanding as follows.
Syntax
jQuery.getJSON(required_url [,specified_info ] [, reques_ success ])
Explanation
By using the above syntax, we can get the ajax JSON response, here we use different parameters with the getJSON method as follows.
- specified_url: The specified url means the actual url that we need to send the request for the response.
- Specified_info: This parameter is optional when we need to send a string or any object to the server at that time we can use this parameter.
- Request_success: This is also an optional parameter and it is used to call back the function, those functions executed successfully.
Examples
Now let’s see the different examples of ajax JSON responses as follows.
First, we need to install the server on vs-code. Once setup is successfully done then we need to follow some steps or we can say that we need to create three different types of files as follows.
The first file we need to create is the JavaScript file for data requests and write the following code as follows.
$(document).ready(() => {
const $S_Data = $('#data');
const $rawdata = $('predata');
$('#getdata').on('click', (ed) => {
ed.preventDefault();
$S_Data.text('JSON file is Loading.');
$.getJSON('sample.JSON', (j_data) => {
const d_j = j_data.records
.map(record => `<li>${ records.Name}: ${ records.ID}</li>`)
.join('');
const j_list = $('<ul />').html(d_j);
$S_Data.html(j_list);
$rawdata.text(JSON.stringify(j_data, undefined, 2));
});
});
});
Explanation
The accompanying code is the finished customer-side rationale. It sits tight for the DOMContentLoaded stacked occasion to fire, prior to getting a reference to two DOM components — $S_Data, where we’ll show the parsed reaction, and $rawdata, where we’ll show the total reaction.
We then, at that point, join an occasion controller to the snap occasion of the component with the ID getdata. At the point when this component is clicked, we endeavour to stack the JSON from the worker utilizing $.getJSON().
Other than changing pieces of the article over to an unordered rundown, the full item is additionally stringified and shown on the screen. The unordered rundown is added to a <div> component with the ID S_data, the JSON string a <predata> tag, so it is well designed. Obviously, for our model the information is fixed, however overall any sort of reaction is conceivable.
Note that we likewise set some text for the yield <div>. On the off chance that we embed a few (fake) delays for the JSON recovery (for instance, in your program’s dev tag), we’ll see that this really executes before any consequence of the $.getJSON demand is shown. The explanation is straightforward:
naturally, $.getJSON is non-hindering — that is, async. Consequently, the callback will be executed at a few (obscure) later points on schedule.
Now we need to create the JSON file and assign the name sample.JSON and write the following code as follows.
{
"records": [
{
"Name": "Jenny",
"ID": 101
},
{
"Name": "Johan",
"ID": 201
},
{
"Name": "Sameer",
"ID": "302"
}
],
"obj": {
"number": 1.2345e-6,
"enabled": true
},
"txt_message": "sample JSON file here"
}
Explanation
The sample JSON document is a lot bigger than the subset we care about. In any case, the example has been developed so as to show the greater part of the JSON punctuation.
In the example JavaScript, we’re just controlling the cluster related to the things key. As opposed to customary JavaScript, JSON expects us to put keys in twofold statements. Furthermore, we can’t utilize the following commas for indicating articles or clusters. Nonetheless, likewise, with common JavaScript clusters, we are permitted to embed objects of various sorts.
Now we need to create the html file for accessing the data as follows.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>response of ajax JSON</title>
</head>
<body>
<a href="#" id="getdata">Click here to get JSON</a>
<div id="sdata"></div>
<script src="https://code.jQuery.com/jQuery-3.6.0.min.js"></script>
<script src="file.js"></script>
</body>
</html>
Explanation
In the above code we use different html tags to integrate all the files such as file.js and sample.JSON file to get the specific result that we want, here we call the jQuery script as shown in the above code. The end output of the above program we illustrated by using the following screenshot as follows.
Now we need to click on the above button that is Click here to get JSON as shown in the above screenshot and after clicking we get the following result as shown in the following screenshot as follows.
Conclusion
We hope from this article you learn more about the jQuery ajax JSON response. From the above article, we have taken in the essential idea of the ajax JSON response and we also see the representation and example of ajax JSON response from this article, we learned how and when we use the jQuery ajax JSON response.
Recommended Articles
This is a guide to jQuery ajax JSON Response. Here we discuss the definition, How to Use jQuery ajax JSON response, examples for better understanding. You may also have a look at the following articles to learn more –