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进行跨域访问的过程中遇到这个问题,梦真帮忙解决了。
/*********************************************************************************
 * 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);
                }
            }
        });

 

目录
相关文章
|
2天前
|
JavaScript 前端开发
has been blocked by CORS policy: The ‘Access-Control-Allow-Origin‘ header contains multiple values ‘
has been blocked by CORS policy: The ‘Access-Control-Allow-Origin‘ header contains multiple values ‘
56 0
remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2
remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2
3688 0
|
5月前
|
安全 JavaScript 前端开发
No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
|
API
【已解决】No ‘Access-Control-Allow-Origin‘ header is present on the requested resource
No ‘Access-Control-Allow-Origin‘ header is present on the requested resource
280 0
【已解决】No ‘Access-Control-Allow-Origin‘ header is present on the requested resource
|
10月前
|
前端开发 JavaScript
ajax请求的重定向处理--Request header field x-requested-with is not allowed by Access-Control-Allow-Header
ajax请求的重定向处理--Request header field x-requested-with is not allowed by Access-Control-Allow-Header
324 0
|
JavaScript Java 应用服务中间件
Vue 跨域问题 的几种解决办法 (No ‘Access-Control-Allow-Origin‘ header is present on the requested resource)
Vue 跨域问题 的几种解决办法 (No ‘Access-Control-Allow-Origin‘ header is present on the requested resource)
1613 0
Vue 跨域问题 的几种解决办法 (No ‘Access-Control-Allow-Origin‘ header is present on the requested resource)
|
Web App开发 存储 安全
新版本Chrome同源策略、跨域问题处理No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
新版本Chrome同源策略、跨域问题处理No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
546 0
|
应用服务中间件 API nginx
解决跨域问题 Response to preflight request doesn't pass access control check
解决跨域问题 Response to preflight request doesn't pass access control check
9273 0
|
存储 NoSQL Redis
docker: denied: requested access to the resource is deniedunauthorized: authentication required
docker: denied: requested access to the resource is deniedunauthorized: authentication required
490 0
docker: denied: requested access to the resource is deniedunauthorized: authentication required
|
前端开发 数据可视化 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.
1111 0