No 'Access-Control-Allow-Origin' header is present on the requested resource.

/*********************************************************************************
 * No 'Access-Control-Allow-Origin' header is present on the requested resource.
 * 说明:
 *     在php中使用ajax进行跨域访问的过程中遇到这个问题,梦真帮忙解决了。
 *
 *                                       2016-12-14 深圳 南山平山村 曾剑锋
 ********************************************************************************/

一、参考文档:
    1. XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin
        http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin
    2. 跨域Ajax之ContentType:application/json
        http://www.foreverpx.cn/2016/06/22/cross_content_type/
    3. JQuery 的 ajax 出现Origin null is not allowed by Access-Control-Allow-Origin 解决方法
        http://blog.csdn.net/leon90dm/article/details/8120378

二、错误现象:
    Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 501.

三、跨域ajax访问代码:
    $.ajax({
    url: "http://192.168.1.20/data.php",
    type: "POST",
    contentType:"application/json; charset=utf-8",  //  ---->  问题就在这里了
    data: JSON.stringify(ajaxPostData),
    dataType:"json",
    success: function(data){
        //On ajax success do this
        console.info("success.");
        if (data["status"] == "ok"){
            alert("Settings is Ok. The Machine is rebooting.");
        }
    },
    error: function(xhr, ajaxOptions, thrownError) {
        //On error do this
        console.info("error.");
        if (xhr.status == 200) {

            alert(ajaxOptions);
        }
        else {
            alert(xhr.status);
            alert(thrownError);
        }
    }
});

四、问题原因:
    在使用Ajax跨域请求时,如果设置Header的ContentType为application/json,会分两次发送请求。第一次先发送Method为OPTIONS的请求到服务器,这个请求会询问服务器支持哪些请求方法(GET,POST等),支持哪些请求头等等服务器的支持情况。等到这个请求返回后,如果原来我们准备发送的请求符合服务器的规则,那么才会继续发送第二个请求,否则会在Console中报错。

五、解决办法:
    1. 在ajax访问中不指定contentType:"application/json; charset=utf-8",使用默认的就可以了;
    2. 如下代码:
        $.ajax({
            url: "http://192.168.1.20/data.php",
            type: "POST",
            data: JSON.stringify(ajaxPostData),
            dataType:"json",
            success: function(data){
                //On ajax success do this
                console.info("success.");
                if (data["status"] == "ok"){
                    alert("Settings is Ok. The Machine is rebooting.");
                }
            },
            error: function(xhr, ajaxOptions, thrownError) {
                //On error do this
                console.info("error.");
                if (xhr.status == 200) {
        
                    alert(ajaxOptions);
                }
                else {
                    alert(xhr.status);
                    alert(thrownError);
                }
            }
        });

 

posted on 2016-12-14 10:18  zengjf  阅读(57955)  评论(0编辑  收藏  举报

导航