The prototype is as follows:
Jquery.getjson (URL, [data], [callback]) loads JSON data across domains.
URL: Send the requested address
Data: (optional) Key/value parameters to be sent
Callback: (optional) callback function on successful load
Primarily used for client-side capture of server JSON data. Simple example:
Server script, returning 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, the PHP function is used to json_encode the data that will be returned.
Second: ECHO is returned to the client, not return.
The following is the core client code:
$.getjson.html
Copy Code code as follows:
<script language= "javascript" type= "Text/javascript" src= "./js/jquery.js" ></script>
<script language= "javascript" type= "Text/javascript" >
function Getjs ()
{
$.getjson ("$.getjson.php", {}, Function (response) {
alert (response.age);
});
}
<input type= "button" Name= "btn" id= "btn" value= "test" onclick= "Javascript:getjs ();" />
One thing to note:
Since the return value is encoded in JSON in PHP, you must use Getjson to invoke the PHP file to get the data. At the same time, it can be noted that the data obtained through Getjson has become an array of objects, you can use response.name,response.age very intuitive to get the return value.