ajax getJSON() function


  Translation results:

get

英[get] 美[ɡɛt]

vt. Get; catch; persuade; receive (punishment, etc.)

vt.& vi. Arrive, come

vi. Become; start; try to deal with; obtain benefits or wealth

n. Reproduce, cub; profit

JSON

Object notation; data format; instance; lightweight data exchange format; representation

ajax getJSON() functionsyntax

Function:In jQuery 1.2, you can load JSON data from other domains by using a callback function in the form of JSONP, such as "myurl?callback=?". jQuery will automatically replace ? with the correct function name to execute the callback function. Note: The code after this line will be executed before this callback function is executed.

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

Parameters:

ParameterDescription
url Required. Specifies the URL to which the request will be sent.
data Optional. Specifies the data to be sent to the server along with the request.
success(data,status,xhr) Optional. Specifies the function to be run when the request is successful. Additional parameters: response - Contains the result data from the request status - Contains the status of the request xhr - Contains the XMLHttpRequest object

Description: This function is the abbreviated Ajax function, equivalent to: $.ajax({url: url,data: data, success: callback,dataType: json}); send Data to the server can be appended to the URL as a query string. If the value of the data parameter is an object (map), it will be converted to a string and URL encoded before being appended to the URL. The return data passed to the callback can be Is a JavaScript object, or an array defined in a JSON structure, and is parsed using the $.parseJSON() method.

ajax getJSON() functionexample

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $.getJSON("这里是你的json 文件地址",function(result){
      $.each(result, function(i, field){
        $("p").append(field + " ");
      });
    });
  });
});
</script>
</head>

<body>

<button>获得 JSON 数据</button>
<p></p>

</body>
</html>

Home

Videos

Q&A