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. (I am afraid that I will forget to use it later, so I don't have much explanation. If I really don't understand it, follow my code to give it a try)
I use php at the backend. 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.
View sourceprint ?......
View sourceprint? /* 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;
View sourceprint ?......
Then the front-end processing is completed.
View sourceprint? $ (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 ");
}
});
});
Note that the "& callback =?" passed in must be included in the backend php code? "Is also output, such:
View sourceprint? $ 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.
If you have any questions, you can raise them below. If I have made any mistake, please help me to point it out.