Ajax cross-Domain access error 501

Source: Internet
Author: User

Reference Documentation:

http://www.foreverpx.cn/2016/06/22/cross_content_type/

Problem:

Run the following code to make an error 501

$.ajax ({
            type: "POST",
        URL: "http://192.168.1.202/sensordata.php",

        contentType: ' Application/json; Charset=utf-8 ',
        data:JSON.stringify (ajaxpostdata),
        dataType: ' JSON ',
        success:function (data) {
            // On Ajax success does this
            console.info ("success.");
            if (data["status" = = = "OK") {
                alert ("The Settings is OK.") The Machine is rebooting. ");
            }
        ,
        error:function (XHR, ajaxoptions, thrownerror) {
            //on error do This
            console.info ("error.");
            if (Xhr.status =) {

                alert (ajaxoptions);
            }
            else {
                alert (xhr.status);
                alert (thrownerror);
            }
        }
    );

Workaround:

Remove ContentType: ' Application/json; Charset=utf-8 '

Reason:

4.1 In the Cross-domain, in addition to ContentType for application/x-www-form-urlencoded, Multipart/form-data or text/plain outside, Triggers a request that the browser first sends the method to options.

4.2 For example, your original request was a method method post, and if the Allow attribute in the header returned by the first request does not have a post method,

4.3 So the second request is not sent, the browser console will be an error, telling you that the POST method is not supported by the server.

Related Article

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.