Jquery's $ getjson call and remote retrieval of JSON strings

Source: Internet
Author: User

The Code is as follows:
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
</Head>
<Script type = "text/javascript" src = "jquery-1.7.2.min.js"> </script>
<Script type = "text/javascript">
$ (Function (){
$ ("Button"). click (function (){
$. GetJSON ("ajax6.asp", function (data ){
$ ("# Pig" ).html (data. name );
});
});
});
</Script>
<Body>
<Button> click </button>
<Div id = "pig"> </div>
</Body>
</Html>

Where ajax6.asp is
Copy codeThe Code is as follows:
<%
Response. Write ("{name: 'Peter ', age: 18 }");
%>

Why can't I get it ?, I want to obtain the json of asp.
Key and value must be enclosed in double quotation marks as follows:
Copy codeThe Code is as follows:
Response. Write ("{" "name": "" peter "", "" age "": "18 ″"}")

In jQuery, getJSON is often used to call and obtain a remote JSON string and convert it to a JSON object. If it is successful, the callback function is executed. The prototype is as follows:
JQuery. getJSON (url, [data], [callback]) loads JSON data across domains.
Url: the address where the request is sent
Data: (optional) key/value parameter to be sent
Callback: (optional) callback function for successful Loading
It is mainly used by clients to obtain server JSON data. Simple Example:
The server script returns JSON data:
Copy codeThe Code is as follows:
// $. GetJSON. php
$ Arr = array ("name" => "zhangsan", "age" => 20 );
$ Jarr = json_encode ($ arr );
Echo $ jarr;

Note: first, use the PHP json_encode function to encode the data to be returned before returning to the client. Second, echo rather than return is returned to the client.
The core client code is as follows:
Copy codeThe Code is 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 ();"/>

Because PHP uses JSON encoded return values, you must use getJSON to call the PHP file to obtain data. At the same time, it can be noted that the data obtained through getJSON has become an object array, and the returned values can be intuitively obtained using response. name and response. age.
Jquery provides $. the getJSON method allows us to implement cross-origin ajax requests, but the content on jqueryAPI is too small. How to use $. getJSON: What kind of database should the request website return for $. after getJSON is obtained, I will use an actual example to describe it.
The backend uses php. One of the main functions of the following code is to provide an interface for booking and registration. The data to be imported includes the user name, contact number, and address:
Copy codeThe Code is as follows:
/* Interface for booking registration execution */
Case "yuyue_interface ":
$ Name = trim ($ _ GET ['name']);
$ Phone = trim ($ _ GET ['phone']);
$ Addr = trim ($ _ GET ['addr ']);
$ Dt = date ("Y-m-d H: I: s ");
$ Cb = $ _ GET ['callback'];
If ($ name = "" | $ name = NULL ){
Echo $ cb. "({code:". json_encode (1 )."})";
} Elseif ($ phone = "" | $ phone = NULL ){
Echo $ cb. "({code:". json_encode (2 )."})";
} Elseif ($ addr = "" | $ addr = NULL ){
Echo $ cb. "({code:". json_encode (3 )."})";
} Else {
$ Db-> execute ("insert into tb_yuyue (realname, telphone, danwei, dt, ischeck) values ('$ name',' $ phone', '$ addr ', '$ dt', 0 )");
Echo $ cb. "({code:". json_encode (0 )."})";
}
Exit;
Break;

Then the front-end processing is completed:
Copy codeThe Code is as follows:
$ (Document). ready (function (){
// The following three parameters are required for booking and registration:
Var name = "name"; // varchar type. The maximum length is 8 characters (4 Chinese characters)
Var phone = "phone"; // varchar type, with a length of 11 characters
Var addr = "addr"; // varchar type, with a maximum length of 500 characters)
$. GetJSON ("http: // request website address/data. php? Ac = yuyue_interface & name = "+ name +" & phone = "+ phone +" & addr = "+ addr +" & callback =? ", Function (data ){
If (data. code = 1 ){
// Custom code
Alert ("name cannot be blank ");
} Else if (data. code = 2 ){
// Custom code
Alert ("mobile phone cannot be blank ");
} Else if (data. code = 3 ){
// Custom code
Alert ("the unit cannot be blank ");
} Else {
// Custom code
Alert ("Reservation successful ");
}
});
});

It should be noted that in the backend php code, the passed "& callback =? "Also output, such:
Copy codeThe Code is as follows:
$ Cb = $ _ GET ['callback'];
Echo $ cb. "({code:". json_encode (4 )."})";

The above is a simple $. getJSON test. Through this test, we can learn how to use $. getJSON and How to Make an interface for cross-origin requests by others.

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.