Jquery cross-origin request example sharing

Source: Internet
Author: User

This article mainly introduces examples of jquery cross-origin requests (jquery sends ajax requests). For more information, see

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) the callback function of the key/value parameter callback: (optional) when the load is successful is mainly used by the client to obtain the server JSON data. Simple Example: the server script returns JSON data: the code is as follows: // $. getJSON. php $ arr = array ("name" => "zhangsan", "age" => 20); $ jarr = json_encode ($ arr); echo $ jarr; note two points: first, encode the data to be returned using the PHP json_encode function before returning to the client. Second, echo rather than return is returned to the client. The core client 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: the code is as follows: /* execution interface for booking registration */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; followed by the front-end processing: the code is as follows: $ (document ). ready (function () {// The following three parameters are required for booking registration var name = "name"; // varchar type, with a maximum length of 8 characters (4 Chinese characters) var phone = "phone"; // varchar type, The length is 11-bit var addr = "addr"; // varchar type, with a maximum length of 500 bits (250 Chinese characters) $. getJSON ("http: // request the 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 ("the 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 in the backend php code, the passed "& callback =? "Also output, for example, the Code: $ 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 learns how to create an interface for cross-origin requests from others.

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.