5 Methods of processing JS cross-domain problem summary _javascript skills

Source: Internet
Author: User

The first two days encountered a cross-domain problem of processing, using JSONP can be resolved. (http://www.jb51.net/article/57889.htm)

Recently tidied up again:

1.jsonp.

Ajax request, datatype for JSONP. This form requires a request to be adjusted to return callback ([Json-object]) on the server side. If the service side returns a normal JSON object. Then when debugging, in the Chrome browser console will report "Uncaught syntaxerror:unexpected token" error, in the Firefox browser console will be reported "syntaxerror:missing; Before statement "error.

2.iframe cross domain.

Add an IFRAME element to the page and, when you need to call a GET request, set the iframe src to the URL of the GET request to initiate a call to the GET request.

Copy Code code as follows:

var url = "http://xxx.xxx.xxx?p1=1&p2=2";
$ ("#iframe"). attr ("src", url);//cross domain, use IFRAME

The IFrame method is stronger than the JSONP, besides can handle HTTP request, also can realize JS call across domain.

Processing of SRC attributes for 3.script elements

The src attribute of iframe, IMG, style, script and so on can request resources directly to different domains, JSONP is the simple realization of requesting resources with the script label, so this is the same as the Jsonp essence, which requires the service-side request to return callback ... Form.

Copy Code code as follows:

var url= "Http://xxx.xxx.xxx?p1=1";
var script = document.createelement (' script ');
Script.setattribute (' src ', url);
document.getElementsByTagName (' head ') [0].appendchild (script);

4. Use get processing on the server.

For the business does not have the hard requirements on the front-end processing, you can do a package on the server, and then the server to initiate calls, so that the problem can be resolved across the domain. You can then decide whether the code uses either synchronous or asynchronous mode, depending on whether the request is sent out or if you need to get the return value.

Copy Code code as follows:

private static void Creategethttpresponse (string url, int? timeout, string useragent, cookiecollection cookies)
{
if (string. IsNullOrEmpty (URL))
{
throw new ArgumentNullException ("url");
}
var request = WebRequest.Create (URL) as HttpWebRequest;
Request. method = ' Get ';
if (!string. IsNullOrEmpty (useragent))
{
Request. useragent = useragent;
}
if (timeout. HasValue)
{
Request. Timeout = Timeout. Value;
}
if (Cookies!= null)
{
Request. Cookiecontainer = new Cookiecontainer ();
Request. Cookiecontainer.add (cookies);
}
Request. BeginGetResponse (null,null);//Asynchronous
Return request. GetResponse () as HttpWebResponse;
}

5.flash cross-domain

too cutting-edge = =, then study

Summary: The above 5 kinds of methods are common to solve the problem of JS Cross-domain processing method, and finally a relatively high-end, and so I study clearly and then fill it.

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.