zoukankan      html  css  js  c++  java
  • 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);
                    }
                }
            });
  • 相关阅读:
    Java代码实现依赖注入
    Linux shell脚本的字符串截取
    Android教程:wifi热点问题
    Android framework层实现实现wifi无缝切换AP
    http mimetype为multipart/x-mixed-replace报文
    Realtek 8192cu 支持 Android Hotspot 软ap
    http协议详解
    Android 在一个程序中启动另一个程序(包名,或者类名)
    linux定时器
    进程与线程的一个简单解释(转)
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/6178189.html
Copyright © 2011-2022 走看看