Make a cross-domain Ajax request test through the $.getjson of jquery _jquery

Source: Internet
Author: User
(mainly to leave a spare, afraid of later to use their own but forget, so there is not much explanation, really do not understand the words, according to my code, you also try a bar)
My back end is in PHP, the following code is mainly implemented a function is to provide an appointment registration interface, need to pass the data are: User name, contact telephone number and address
* * Booking Registration Implementation Interface * *
Copy Code code as follows:

* * Booking Registration Implementation Interface * *
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

And then the front end is handled.
Copy Code code as follows:

$ (document). Ready (function () {
The following 3 parameters required for an appointment registration
var name = "Name"; varchar type with a maximum length of 8 digits (4 characters)
var phone = "Phone"; varchar type, 11-bit length
var addr = "Addr"; varchar type with a maximum length of 500 digits (250 characters)
$.getjson ("http://request Web 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 empty");
}else if (data.code==3) {
Custom code
Alert ("The unit cannot be empty");
}else{
Custom code
Alert ("Successful appointment");
}
});
});

It's important to note that in the back-end PHP code, the "&callback=" must be passed in. "Also lose out, such as:
Copy Code code as follows:

$CB = $_get[' callback '];
echo $CB. " ({code: ". Json_encode (4)."});

The above is a simple $.getjson experiment, through this experiment, we can learn how to use $.getjson, also can learn how to do an interface to let others cross domain request.
If you have any questions, you can put them down below and if I'm wrong, please point them out for me.
Related Article

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.