This article mainly introduces three methods of ajax processing cross-origin in jquery. due to the influence of the same-origin JavaScript policy, JavaScript can only access documents under the same domain name. To implement cross-origin, you can use the following methods:
I. Cross-Origin processing method:
1. Proxy
2. XHR2
XMLHTTPREQUEST Level2 (and XHR2) provided in HTML5. However, ie10 or lower does not support
You only need to fill in the response header on the server:
Header ("Access-Control-Allow-Origin: *");/* indicates that all domains are acceptable. */header ("Access-Control-Allow-Methods: GET, POST ");
3. jsonP
Principle:
Ajax itself cannot cross-origin,
Cross-origin is implemented by generating a script tag. Because the src attribute of the script tag has no cross-origin restrictions.
In fact, after the Ype: 'jsonp' is set, the $. ajax method has nothing to do with ajax XmlHttpRequest. Instead, the jsonp protocol is used. JSONP is an unofficial protocol that allows the server to integrate Script tags to return to the client and implement cross-origin access through javascript callback.
Cross-Origin ajax syntax:
(The rest of the write method is the same as that of non-Cross-origin ):
For example
/* The current URL is localhost: 3000 */js code $. ajax ({type: "get", url: "http: // localhost: 3000/showAll",/* url: write an exotic request address */dataType: "jsonp ", /* Add Ype */jsonpCallback: "cb",/* set a callback function. The name can be obtained at will, just like the name in the following function */success: function () {... });/* On an exotic server, */app. js app. get ('/showall', students. showAll);/* This is the same as that for non-Cross-Origin operations * // * in the showAll function of a foreign server, */var db = require (". /database "); exports. showAll = function (req, res) {/** set the response header to allow ajax cross-origin access **/res. setHeader ("Access-Control-Allow-Origin", "*");/* indicates that all requests from other countries are acceptable. */res. setHeader ("Access-Control-Allow-Methods", "GET, POST"); var con = db. getCon (); con. query ("select * from t_students", function (error, rows) {if (error) {console. log ("database error:" + error);} else {/* Note that the returned result is jsonP's callback function name + data */res. send ("cb (" + JSON. stringify (r) + ")");}});}
II. Cross-Origin ajax access and JQuery methods
Cross-Origin of JS, I think many programmers still think that JS cannot be cross-origin. In fact, this is a wrong idea. many people find a solution on the Internet, there are many articles that teach you how to use IFRAME. is it so complicated? In fact, it is very simple. if you use JQUERY, a GETJSON method will be done, and a line of code will be done.
Paste the method below.
// Cross-domain (across all domain names) $. getJSON ("http://user.hnce.com.cn/getregion.aspx? Id = 0 & jsoncallback =? ", Function (json) {// the data format of the remote request page is required :? (Json_data) // for example ://? ([{"_ Name": "Hunan province", "_ regionId": 134 },{ "_ name": "Beijing", "_ regionId": 143}]) alert (json [0]. _ name) ;}); $. getJSON ("http://user.hnce.com.cn/getregion.aspx? Id = 0 & jsoncallback =? ", Function (json) {// the data format of the remote request page is required :? (Json_data) // for example ://? ([{"_ Name": "Hunan province", "_ regionId": 134 },{ "_ name": "Beijing", "_ regionId": 143}]) alert (json [0]. _ name );});
Note: getregion. in aspx, when outputting JSON data, you must use Request. queryString ["jsoncallback"] puts the obtained content before the returned JSON data. if the actually obtained value is 42342348, the returned value is 42342348 ([{"_ name ": "Hunan province", "_ regionId": 134 },{ "_ name": "Beijing", "_ regionId": 143}])
Because the cross-origin principle of getJSON is? Randomly change the method name and return the executed method to achieve cross-origin response.
The following is a real example of cross-origin execution: