Home > Web Front-end > JS Tutorial > body text

Instructions for using $.getJSON in JQuery_jquery

WBOY
Release: 2016-05-16 18:09:44
Original
1061 people have browsed it

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.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!