jQuery getJSON()

Introduction

Get JSON data using an AJAX request, and output the result:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*from   w ww .  ja  va 2s .co m*/
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.getJSON("ajax-json.js", function(result){
      $.each(result, function(i, field){
        $("div").append(field + " ");
      });
    });
  });
});
</script>
</head>
<body>

<button>Get JSON data</button>

<div></div>

</body>
</html>

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

$(selector).getJSON(url,data,success(data,status,xhr))
Parameter
Optional
Description
url
Required.
the url to send the request to
data
Optional.
data to be sent to the server
success(data,status,xhr)









Optional.









the function to run if the request succeeds
Additional parameters:
data - contains the data returned from the server.
status - contains a string containing request status
"success",
"notmodified",
"error",
"timeout",
"parsererror".
xhr - contains the XMLHttpRequest object



PreviousNext

Related