Questions about Access Control allow Origin error

Source: Internet
Author: User
Tags script tag

The first time the day before yesterday a GET request was sent to the server using jquery. The result was a sucking-up. Check the console tip saying "No ' Access-control-allow-origin ' header is present on the requested resource. Origin ' null ' is therefore not allowed access. "Yes, well, Google didn't know it was a cross-domain issue.

Cross-domain access: AJAX cross-domain access is the cross-domain access request to the B site that is generated when a user accesses a site, which is submitted to the specified page of site A-Baidu encyclopedia

and search some articles, my understanding is that JavaScript has the same origin policy restrictions, JS file can only operate to obtain the same domain name, the same port data. So when I open the HTML locally and make a request to the server, it is rejected at first-it's no problem to put the HTML file you wrote on the server and open it.

(This is not very troublesome, every time you modify the point of everything must be submitted to the test??? )

Fortunately, jquery encapsulates the functionality of the JSONP.

About Jsonp (JSON with Padding)

Note that JSON and JSONP differ greatly! JSON is a lightweight data format, and JSONP can be understood as a protocol. The above mentioned JavaScript has a homologous strategy, but it does not make physical damage to some tags (script, iframe, IMG, etc. with SRC attribute tags). The core of JSONP is to use the script tag to invoke the JS script provided by the server, while JSON is native to JavaScript, so the JSONP is to treat the resulting JSON data as javascrip objects, so there is no cross-domain problem.

Jsonp How to use JavaScript Demo
    //function called after the server returns results    var Callback = function(Data){        Console.Log(Data);    };    //provide the URL address of the JSONP service (regardless of the type of address, the resulting return value is a piece of JavaScript code)    var URL = "Http://yoururl.com";    //Create a script tag, set its properties    var Script = Document.createelement(' script ');    Script.SetAttribute(' src ', URL);    //Add the script tag to head, at which point the call begins    Document.getElementsByTagName(' head ')[0].appendchild(Script); 
jquery Demo
$.Ajax({        type: "Get",        URL:"Http://yoururl.com",        DataType: "Jsonp",        Data: {        },        //When the server returns and calls the Success function directly, it is not necessary for us to define the callback.         Success: function(Object) {            Console.Log(Object);        }})
Recommended reading:
    • Talking about cross-domain

    • Talk about JSON and JSONP, maybe you'll be enlightened, with jquery use cases

    • Baidu Encyclopedia--jsonp

Questions about Access Control allow Origin error

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.