Home > Web Front-end > JS Tutorial > Summary of 5 ways to deal with js cross-domain problems_javascript skills

Summary of 5 ways to deal with js cross-domain problems_javascript skills

WBOY
Release: 2016-05-16 16:29:16
Original
1486 people have browsed it

I encountered a cross-domain problem two days ago, which can be solved using jsonp. (http://www.jb51.net/article/57889.htm)

Recently sorted it out:

1.jsonp.

Ajax request, dataType is jsonp. This form requires the request to be adjusted on the server side to return callback([json-object]). If the server returns a normal json object. Then when debugging, the "Uncaught SyntaxError: Unexpected token" error will be reported in the chrome browser console; the "SyntaxError: missing; before statement" error will be reported in the firefox browser console.

2.iframe cross-domain.

Add an iframe element to the page. When you need to call a get request, set the src of the iframe to the url of the get request to initiate the get request call.

Copy code The code is 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 jsonp. In addition to processing http requests, it can also implement js calls across domains.

3. src attribute processing of script elements

The src attribute of iframe, img, style, script and other elements can directly request resources from different domains. jsonp is a simple implementation of using script tags to request resources across domains, so this is essentially the same as jsonp and also requires server-side request return callback... form.

Copy code The code is 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 business that does not have rigid requirements for front-end processing, you can encapsulate it on the server side and then initiate a call on the server side, so that cross-domain problems can be solved. Then depending on whether the request is sent out or whether the return value needs to be obtained, the code uses synchronous or asynchronous mode.

Copy code The code is 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);//异步
            //return request.GetResponse() as HttpWebResponse;
        }

5.flash跨域

过于尖端了==,再研究

总结:以上5种方法就是常见的解决js跨域问题的处理方法了,最后一种比较高端,等我研究清楚了再补上吧。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template