The
jQuery.getJSON( url, [data], [callback] )
method is used to load JSON information from the server by using a GET HTTP request. The GET HTTP request method will return
XMLHttpRequest object
. Following is the syntax to use the GET HTTP request method.
[xml]$.getJSON( url, [data], [callback] )[/xml]
Where, The data parameter will have key, value pairs and the callback is the another parameter to represent the function to be executed. Save the following code as
result.json
.
[html]
{
"name": "Zara Ali",
"age" : "67",
"sex": "female"
}
[/html]
Following is the HTML file to call the JSON file.
[html]
<html>
<head>
<title>The jQuery Example</title>
<script type = "text/javascript"
src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript" language = "javascript">
$(document).ready(function() {
$("#driver").click(function(event){
$.getJSON('result.json', function(jd) {
$('#stage').html('<p> Name: ' + jd.name + '</p>');
$('#stage').append('<p>Age : ' + jd.age+ '</p>');
$('#stage').append('<p> Sex: ' + jd.sex+ '</p>');
});
});
});
</script>
</head>
<body>
<p>Click on the button to load result.html file:</p>
<div id = "stage" style = "background-color:#cc0;">
STAGE
</div>
<input type = "button" id = "driver" value = "Load Data" />
</body>
</html>
[/html]
The
ready
event happens when the DOM has been stacked. Since this event happens after the document is prepared, it is a decent place to have all other jQuery functions and events. The ready() strategy determines what happens when a ready event happens.
The
click
event happens when a component is clicked and click() strategy triggers the click occasion, appends a capacity to run when a click event happens. The append() technique embeds determined substance toward the end of the chose components.
The
append()
technique embeds determined substance toward the end of the chose components.
Output: Now compile the code result will be as follows.