jquery cross-domain request sample sharing

Source: Internet
Author: User
Tags json php file php code trim

  This article mainly introduces the jquery Cross-domain request example (jquery sends Ajax request), needs the friend to be possible to refer to under

A common Getjson in jquery to invoke and get a remote JSON string, convert it to a JSON object, and execute the callback function if successful. The prototype is as follows:   Jquery.getjson (URL, [data], [callback]) loads JSON data across domains.   URL: Send the requested address data: (optional) to be sent Key/value parameter callback: (optional) The callback function when the load succeeds is primarily used for the client to get the server JSON data. Simple example:   server script, return JSON data:     Code as follows://$.getjson.php $arr =array ("name" => "Zhangsan", "Age" =>20);   $jarr =json_encode ($arr);  echo $jarr;     Note two: first: Before returning to the client, use the PHP function to json_encode the data that will be returned. Second: ECHO is returned to the client, not return.   Below is the core client code: The   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) {                  &N Bsp;alert (response.age);   }); }   <input type= "button" Name= "btn" id= "btn" value= "test" Onclick= "Javascript:getjs ();" />     Because in PHP you are using JSONEncoded return value, so 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.   jquery provides a $.getjson approach that allows us to implement Cross-domain Ajax requests, but Jqueryapi is too small, how to use $.getjson to request a Web site to return what kind of database to get $.getjson access to, I will use a practical example to illustrate the following.   back end is in PHP, the following code is mainly implemented a function is to provide an appointment registration of the interface, need to pass the data are: User name, contact telephone number and address:     Code as follows:/* Booking Registration Implementation Interface * * 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     followed by the front-end processing:   code is as follows: $ (document). Ready (function () { //The following 3 parameters  var name = "Name" required for the booking registration; &nbs P;//varchar type, with a maximum length of 8 digits (4 characters)  var phone = "Phone"; varchar type, length of 11-bit  var addr = "addr";  //varchar type with a maximum length of 500 bits (250 kanji)  $.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 ("Cell phone cannot be empty");  }else if (data.code==3) {  & nbsp;//Custom code    alert ("The unit cannot be empty");  }else{   //custom code    alert ("successful appointment");  }   });     Note that in the back-end PHP code, must pass in the "&callback=?" "also lost, such as:   code as follows: $CB = $_get[' callback ']; echo $CB. " ({code: ". Json_encode (4)."});   above is a simple $.getjson test, through this experiment, we can learn how to use $.getjson, also can learn how to do an interface to let others cross domain request.

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.