Home >Web Front-end >JS Tutorial >Instructions for using $.getJSON in JQuery_jquery
The prototype is as follows:
jQuery.getJSON( url, [data], [callback] ) loads JSON data across domains.
url: the address to send the request
data: (optional) key/value parameters to be sent
callback: (optional) callback function when loading is successful
is mainly used for the client to obtain the server JSON data. Simple example:
Server script, return JSON data:
$.getJSON.php
$arr=array("name"=>"zhangsan", "age"=>20);
$jarr=json_encode($arr);
echo $jarr;
Note two points:
First: Before returning to the client, use the PHP function json_encode to encode the returned data.
Second: echo is used to return to the client, not return.
The following is the core client code:
$.getJSON.html