我正在尝试在离线应用程序中使用铬进行跨域Ajax调用。 我想在我的API做一个POST和Nginx不断拒绝我的OPTIONS 。
XMLHttpRequest cannot load http://wss.dev:8080/api/checkin. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 405.
我尝试了不同的configuration,但似乎没有任何工作。
我的conf是一个跟随:
server { listen *:8080; server_name wss.dev www.wss.dev; client_max_body_size 200m; index index.html index.htm index.php; access_log /var/log/nginx/wss.dev.access.log; error_log /var/log/nginx/wss.dev.error.log; add_header 'Access-Control-Allow-Origin' $http_origin; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET, POST, PUT, DELETE' ; add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With,XMLHttpRequest'; location ~ .php$ { root /var/www/public; try_files $uri $uri/ /index.php /index.php$is_args$args$is_args$args; index index.html index.htm index.php; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_param APPLICATION_ENV dev; fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; } location / { root /var/www/public; try_files $uri $uri/ index.php /index.php$is_args$args; } sendfile off; }
我尝试在这个例子中: https : //michielkalkman.com/snippets/nginx-cors-open-configuration.html
我尝试了不同的configuration,我试图改变我所有的angular度,但没有任何作品…
没有反应可能是由类似这样的configuration造成的:
if ($request_method !~ ^(GET|HEAD|POST|PUT|DELETE)$ ) { return 444; }
这意味着nginx会在没有正确响应的情况下closures连接,这就是为什么debugging非常困难的原因。
search您的configuration444返回码。 如果您发现上述内容,只需将OPTIONS添加到列表中)。
你是否想从本地开发箱发布? Chrome有一个不支持localhost的CORS请求的bug ,将其更改为“mylocalbox.dev”,它应该工作。