来自nginx的带有422状态代码的响应缺少'Access-Control-Allow-Origin'标头 [英] Response with 422 status code from nginx lacks 'Access-Control-Allow-Origin' header

查看:873
本文介绍了来自nginx的带有422状态代码的响应缺少'Access-Control-Allow-Origin'标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

nginx配置如下:

nginx config as follows:

server {
            listen 80;
            listen [::]:80;

            add_header 'Access-Control-Allow-Origin' $http_origin;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT, PATCH';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
            add_header 'Access-Control-Max-Age' 1728000;

            server_name erp.dev.thinkerx.com;
            access_log  /home/thinkerx/nginx/access.log;
            error_log /home/thinkerx/nginx/error.log;
            location ~ /.well-known {
                    allow all;
            }

            # The rest of your server block
            root /usr/share/nginx/html/men2017-back-dev/public;
            index index.html index.htm index.php;

            location /api/ {
                    try_files $uri $uri/ /index.php$is_args$args;
            }

            location ~ \.php$ {
                            try_files $uri /index.php =404;
                            fastcgi_pass 127.0.0.1:9000;
                            fastcgi_index index.php;
                            fastcgi_buffers 16 16k;
                            fastcgi_buffer_size 32k;
                            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                            include fastcgi_params;
                    }
    }

js代码如下:

$.ajax({
    type: 'post',
    dataType: 'json',
    contentType: 'application/json; charset=UTF-8',
    url: "http://erp.dev.thinkerx.com/api/external/material/catalogs",
    data: JSON.stringify({
        domain_id: 2222,
        code:'X01',
        name:'123063'
    }),
    success: function (response) {
        console.log(response);      
    },
    error: function (xhr, status, error) {
        console.log(xhr, status, error);    
    },
});

然后,在浏览器中发送请求,Chrome控制台显示两个请求.第一个请求是预检,方法是 OPTION .第二个是实际请求并具有响应,其状态码为201. 预检请求

then, send request in browser, chrome console shows two request. The first request is preflight, method is OPTION. The second is real request and has response, whose status code is 201. preflight request

{"data":{"id":"16b7d6a0-9eb6-42ca-9ddb-fc61f5e082c0","domain_id":2222,"name":"1230464","code":"X01","parent_id":null,"created_at":1504698369,"updated_at":1504698369}}

如上所述,这是预料之中的事,但我会更新ajax数据.

As above, the thing is expected, but i update ajax data.

$.ajax({
    type: 'post',
    dataType: 'json',
    contentType: 'application/json; charset=UTF-8',
    url: "http://erp.dev.thinkerx.com/api/external/material/catalogs",
    data: JSON.stringify({
        domain_id: 2222,
        code:'X01',
        // name:'123063'
    }),
    success: function (response) {
        console.log(response);      
    },
    error: function (xhr, status, error) {
        console.log(xhr, status, error);    
    },
});

我再次发送请求.偶然地,发生了错误. 还有两个请求,第二个状态代码是422

i send the request again.Accidentally, the error occurred. also two requests, the second status code is 422

{消息":验证失败",错误":[[密钥名称必须为 目前]],"状态码:422}

{"message":"Validation Failed","errors":[["Key name must be present"]],"status_code":422}

XMLHttpRequest无法加载 http://erp.dev.thinkerx.com/api/external/material/目录.不 请求中存在"Access-Control-Allow-Origin"标头 资源.因此,不允许访问来源' http://localhost . 响应的HTTP状态代码为422.

XMLHttpRequest cannot load http://erp.dev.thinkerx.com/api/external/material/catalogs. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 422.

我有一些问题,如下:

  1. 为什么报告相同的原产地政策错误?
  2. 我在控制台中看到响应,为什么未定义ajax xhr.responseJSON?如何获取响应?

推荐答案

我遇到了同样的问题.问题是nginx仅为200、204、301、302和304状态码添加标头.

I had same issue. Problem is that nginx add header only for 200, 204, 301, 302 and 304 status codes.

要为每种类型的状态码获取相同的标头,必须像这样在add_header指令的末尾添加[always].

To get same headers for every type of status codes you have to add [always] in the end of add_header directive like this.

add_header 'Access-Control-Allow-Origin' $http_origin always;

希望对您有帮助)

这篇关于来自nginx的带有422状态代码的响应缺少'Access-Control-Allow-Origin'标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆