Home >Web Front-end >JS Tutorial >Instructions for using $.getJSON in JQuery_jquery

Instructions for using $.getJSON in JQuery_jquery

WBOY
WBOYOriginal
2016-05-16 18:09:441134browse

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

Copy code The code is as follows:


< ;script language="javascript" type="text/javascript">
function getjs()
{
$.getJSON("$.getJSON.php", {}, function(response){
alert(response.age);
});
}


Note:
Since the return value is encoded in JSON in PHP, you must use getJSON to call PHP here file to obtain data. At the same time, you can notice that the data obtained through getJSON has become an object array, and you can use response.name and response.age to get the return value intuitively.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn