To make JSON call we use getJSON method. Here is the syntax and description of it.
Syntax :
$.getJSON( url, [data], [callback] )
Description
1. url : It is URL where the request is send and which emits out JSON.
2. data : this parameter helps us to pass data to the server.
3. callback : this parameter is a function which gets invoked when data is returned and the data gets loaded successfully.
Below is the small example of using it:
I am assuming that the response.json file contains the following content -
{
emp-name": ABCD,
emp-age" : "67",
emp-code: 4567
}
Now below code is the example that shows the usage of this method-
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$(#demo).click(function(event){
$.getJSON('response.json', function(data) {
$(#eg1).html('<p>Name: ' + data.name + '</p>');
$('#eg1).append('<p>Age : ' + data.age+ '</p>');
$('#eg1').append('<p>Code : ' + data.code+ '</p>');
});
});
});
</script>
</head>
<body>
<p>Click the button:</p>
<div id=eg1 style="background-color:#dddd;>
Example
</div>
<input type="button" id=demo value=Show Data" />
</body>
</html>
This is how you can simply make a JSON call using jquery.
0 Comment(s)