getJSON() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:getJSON

Description

The getJSON() method gets JSON data using an AJAX HTTP GET request.

Syntax

$.getJSON(url,data,function(data,status,xhr))
ParameterRequire Description
url Required. 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

success(data,status,xhr) parameters:

  • data - data returned from the server.
  • status - string containing request status ("success", "notmodified", "error", "timeout", or "parsererror").
  • xhr - the XMLHttpRequest object

The following code shows how to Get JSON data using an AJAX request, and output the result:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.getJSON("demo_ajax_json.js", function(result){
            $.each(result, function(i, field){
                $("div").append(field + " ");
            });//from www  . j  a v  a  2s  . com
        });
    });
});
</script>
</head>
<body>

<button>test</button>

<div></div>

</body>
</html>

Related Tutorials