JS-cross-domain

Source: Internet
Author: User

cross-domain error: xmlhttprequest cannot load ' url '. No ' Access-control-allow-origin ' header is present on the requested resource. Origin ' URL ' is therefore not allowed access.

What is cross-domain: protocol, subdomain, primary domain, port number, any one of which is not the same, counted as cross-domain.

Cause: JS is not allowed to invoke objects of other pages across domains for security reasons. (same-Origin policy limit)

Workaround:

1. Agent: by creating a proxy on the Web server side of the same domain name

Own:http://www.abc.com

Others ':http://www.bcd.com

When you access your own server (http://www.abc.com/proxy-service.php), your own server (http://www.abc.com/proxy-service.php) calls someone else's service ( http://www.bcd.com/service.php) so that the front-end calls its own server, which is equivalent to calling someone else's server.

2, JSONP: (JSONP cross-domain request support GET request)

Jsonp can be used to address cross-domain data access issues in mainstream browsers:

The server JS file to load the cross-domain JS file, cross-domain JS file return JSONP, is to avoid cross-domain problems.

Front-end Code: $.ajax ({    type: "Get",    //Use JSONP cross-domain request only support GET request    URL: "Cross domain Address",    dataType: "Jsonp",    Jsonp: " Callback ",    success:function () {        if (data.sueccess) {            //parse Jsonp        }else{        }    },    error: function (JQXHR) {        Console.log ("Error occurred:" +jqxhr.status);    

3, XHR2:

The XMLHttpRequest Level2 provided by HTML5 has implemented cross-domain access and a number of other new features

IE10 versions below are not supported

• Make some minor changes on the server side: (PHP)

Header (' access-control-allow-origin:* ');

Header (' Access-control-allow-methods:post,get ');

JS-cross-domain

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.