Home >>jQuery Tutorial >jQuery $.getJSON() Method

jQuery $.getJSON() Method

jQuery $.getJSON() Method

jQuery $.getJSON() method in jQuery is used to get JSON data using an AJAX HTTP GET request.

Syntax:
$(selector).getJSON(url,data,success(data,status,xhr))

Parameter Values

Parameter Description
url It is a Required parameter and is used to specifies the url to send the request to
data It is an Optional parameter and is used to specifies data to be sent to the server
success(data,status,xhr) It is an Optional parameter and is used to specifies the function to run if the request succeeds
Additional parameters:
  • data – It is used to contains the data returned from the server.
  • status – It is used to contains a string containing request status ("success", "notmodified", "error", "timeout", or "parsererror").
  • xhr – It is used to contains the XMLHttpRequest object
Here is an Example of jQuery getJSON() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$.getJSON("demo_ajax_json.js", function(result){
$.each(result, function(i, field){
$(".box1").append(field + " ");
});
});
});
});
</script>
</head>
<body>
<button class="btn1">Click me to Get JSON data</button>
<div class="box1"></div>
</body>
</html>

Output:

No Sidebar ads