访问控制允许Origin在Chrome的邮递员上工作但不在应用程序

时间:2016-05-11 10:07:27

标签: symfony nginx http-headers cors

我在symfony之上构建一个关于react和一个RESTful API后端的UI。

我已经通过邮递员对api进行了测试并且连接正常,但是当我尝试通过我的应用程序进行连接时,连接失败并且它说  No' Access-Control-Allow-Origin'标头出现在请求的资源上。

这是我后端的nginx配置的一部分

  location / {
if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' *;
    add_header 'Access-Control-Allow-Methods' 'GET, POST';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    add_header 'Content-Type' 'text/plain charset=UTF-8';
     add_header 'Content-Length' 0;
    return 204;
}
if ($request_method = 'POST') {
    add_header 'Access-Control-Allow-Origin' *;
    add_header 'Access-Control-Allow-Methods' 'GET, POST,PUT, DELETE, PATCH';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}

if ($request_method = 'GET') {
    add_header 'Access-Control-Allow-Origin' *;
    add_header 'Access-Control-Allow-Methods' 'GET, POST,PUT, DELETE, PATCH';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
 try_files $uri /app_dev.php$is_args$args;


 }

location ~ ^/(app_dev|config)\.php(/|$) {
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    try_files $uri $uri/ index.php /index.php$is_args$args;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;

    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $realpath_root;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;

}
}

请注意,预检OPTIONS请求可以正常使用以下响应

Access-Control-Allow-Headers:DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type
Access-Control-Allow-Methods:GET, POST
Access-Control-Allow-Origin:*

我错过了什么吗?

0 个答案:

没有答案