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);
                }
            }
        });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM