How to use getJSON() Method in JQuery AJAX

In this article, I will explain use of getJSON() Method in JQuery AJAX.
  • 3584

Query AJAX getJSON() Method

  • getJSON() method is used load JSON data from the server using an AJAX HTTP GET request.

Syntax

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

Parameter

  • url: url is required parameter. A string containing the URL to which the request is sent
  • data: data is optional parameter. data to send to the server when request was found.
  • Success(data,status,xhr) this is the optional parameter. the function to run if the request completed
    Additional parameters:

    • data - data contains the data returned from the server.
    • status - status contains a string containing request status.
    • xhr - xhr contains the XMLHttpRequest object

Example

 

The following example show the use of getJSON() method.

 

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

    $(document).ready(function () {

        $("button").click(function () {

            $.getJSON("JScript.js", function (result) {

                $.each(result, function (i, field) {

                    $("div").append(field + " ");

                });

            });

        });

    });

</script>

</head>

<body>

<button>Click for get JSON data</button>

<div></div>

</body>

</html>

Output

getjson.jpg 

You may also want to read these related articles Click here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.