A summary of cross-domain get, POST requests

Source: Internet
Author: User

Focus: Post large amounts of data across domains;

Jquery:$.ajax/$.getjson support JSONP format cross-domain, but only support get mode, temporarily does not support post;

CORS:W3C on the cross-domain of the new scheme, Res.setheader (' Access-control-allow-origin ', ' * '), compatible with ie9+;

So, cross-domain post is a problem worth studying! Magnum jquery cannot cross-domain post; In view of the basic national conditions, cors is only suitable for mobile play;

I believe some students have done this:

1         $.ajax ({2Type: ' Post ',3 Url:url,4Data: {A:1,b:2},5DataType: "Jsonp",6Crossdomain:true,7Jsonpcallback: "Succallback",8Successfunction(data) {9 console.log (data);Ten             }, OneErrorfunction(data) { A console.log (data); -             } -});

In fact, the Chrome console shows this:

So, is it possible to prove that jquery does not cross-domain post? The good post, jquery automatically turn it into get, this is actually similar to the $.getjson, just $.ajax cross-domain directly can callback return value, and $.getjson return value needs a global callback (can be customized);

Say my personal station in the mobile side is still in the most original zoom state, good H5 Response the Web? Forgive me for running off again: originally planned <link rel= "stylesheets" type= "Css/text" media= "screen and (max-width:xx-px)" href= "Max-xx.css"/> or directly in the original style table media query, and the result is I opened a new site, as a mobile version, read the data stored on the PC, on the one hand, to reduce the amount of code, so that the mobile side lighter, simple, after all, just a blog it, on the other hand, the mobile side will have a wider range of free space In addition, based on performance optimization, this is good; Of course, the problem is obvious; two sites to share data, together with a database, to do synchronous operation, hehe, I a small front-end, these seem to think too much ah! So what to do, yes, that is the problem mentioned above, the front-end cross-domain request data;

The mobile side of the library is a version of a custom assembly by some of Zepto's modules, bootstrap what cool, style handwritten, right when practiced hand bar ... At the back end, the Nodejs is made by the Http-proxy agent, judging the UA of the request, if the access is mobile, Whether the host is www.famanoder.com or m.famanoder.com all distributed to the mobile site processing, so that on the phone to access http://www.famanoder.com do not have to do the jump;

OK, then to start cross-domain request and submit data, if only to request remote data, get way enough, but if you want to submit a large amount of data, for example, a warrior passing a small station, a moment rise, classics, ancient and modern, gushing, long-winded a pass, so I am afraid to get confused Ah! Let's look at the simplest get method:

1 $.getjson (' http://www.famanoder.com/page/ajaxPage?callback=?&jsonpCallback=getmore&page=3 &ori=about ');

Then define the overall getmore is OK, back-end processing is also simple, I this is Nodejs, other self-Baidu bar:

1Artical.find ({},function(err,docs) {2err&&Console.log (err);3     varD=json.stringify (docs);4Res.send (callback+ "(" +d+ ")");5});

Of course, like that is also possible, the back end of the same treatment;

As I said before, the great jquery does not support the cross-domain of post mode for the time being. In fact, it is not as complicated as it is, it is time to prove that you can't rely too much on jquery, sometimes without her, you have to do well;

The basic principle is like this: Form form submission, whether you are post, or get, regardless of where you submit to, as long as the backend has a corresponding program processing, are duly completed, there is no cross-domain one said; no previous refresh upload, submit form is the target of the form to an empty IFRAME, where the same ; Similar: A.com submits the form to B.Com, the form's target is an empty iframe;b.com processing request, callback is executed in response, note that the callback here belongs to the empty iframe;

1<!--a.html--2<!--test post access domain--3<!--use form and hide iframe---4<form action= "B.Com" method= "post" target= "Hidefrm" >5<input type= "text" value= "test post data access domain" >6<input type= "Submit" value= "Submit" >7</form>8<iframe src= "" Name= "hidefrm" frameborder= "0" ></iframe>

The back end of the process is basically the same, if not expected, to this, the request will appear in the console a red, yes, you want to set up domain, in addition to the page and the corresponding JS to set domain, must not forget, response in the callback is actually belong to the current page, So the callback to be implemented in the response should also be set to domain, similar to this:

1 res.send (' document.domain= ' a.com; Window.parent.aa (' +str+ ') ');

A summary of cross-domain get, POST requests

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.