$.getjson usage instructions in jquery _jquery

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.