簡體   English   中英

angular js nginx cors-沒有'Access-Control-Allow-Origin'標頭

[英]angular js nginx cors - No 'Access-Control-Allow-Origin' header

我知道有人問過同一問題的不同版本,我已經閱讀了所有相關的問題和解答,可以在SO和Google上找到。

我正在嘗試從angularJS應用向nginx服務器發出跨域請求。 所有GET請求的行為均正常,但我的POST請求卻不正常。

Chrome給我一個'Access-Control-Allow-Origin'錯誤,因此我在nginx服務器配置中添加了以下內容:

location / {

if ($http_origin ~* (https?://.*\.mysite\.com(:[0-9]+)?)) {
    set $cors "true";
}

if ($request_method = 'OPTIONS') {
    set $cors "${cors}options";  
}

# non-OPTIONS indicates a normal CORS request
if ($request_method = 'GET') {
    set $cors "${cors}get";  
}
if ($request_method = 'POST') {
    set $cors "${cors}post";
}

# if it's a GET or POST, set the standard CORS responses header
if ($cors = "trueget") {
    add_header 'Access-Control-Allow-Origin' "$http_origin";
    #add_header 'Access-Control-Allow-Credentials' 'true';
}

if ($cors = "truepost") {
    add_header 'Access-Control-Allow-Origin' "$http_origin";
    #add_header 'Access-Control-Allow-Credentials' 'true';
}

if ($cors = "trueoptions") {
    add_header 'Access-Control-Allow-Origin' "$http_origin";
    #add_header 'Access-Control-Allow-Credentials' 'true';

    #
    # Return special preflight info
    #        
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since';
    add_header 'Content-Length' 0;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    return 204;
}
}

OPTIONS請求返回狀態204和預期的已添加標頭:

Request URL:<code>http://otherSite/shfofx/PHP/Add/addTrade</code>
Request Method:OPTIONS
Status Code:204 No Content
Request Headers
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, x-requested-with, content-type
Access-Control-Request-Method:POST
AlexaToolbar-ALX_NS_PH:AlexaToolbar/alxg-3.2
Cache-Control:no-cache
Connection:keep-alive
Host:otherSite
Origin:<code>http://test-shf.mysite.com</code>
Pragma:no-cache
Referer:<code>http://test-shf.mysite.com/portfolio</code>
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like    Gecko) Chrome/31.0.1650.63 Safari/537.36
Response Headersview source
Access-Control-Allow-Headers:Authorization,Content-Type,Accept,Origin,User-   Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Requested-With,Keep-Alive,If-Modified-Since
Access-Control-Allow-Methods:GET, POST, OPTIONS
Access-Control-Allow-Origin:<code>http://test-shf.mysite.com</code>
Access-Control-Max-Age:1728000
Connection:keep-alive
Content-Length:0
Content-Type:text/plain charset=UTF-8
Date:Thu, 02 Jan 2014 22:54:58 GMT
Server:nginx/1.2.6 (Ubuntu)

然后,我在Chrome的“網絡”標簽中看到帶有POST請求的網絡呼叫,該請求已通過以下控制台輸出取消:

XMLHttpRequest cannot load <code>http://otherSite/shfofx/PHP/Add/addTrade</code>. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://test-shf.mysite.com' is therefore not allowed access.

取消的POST請求的標頭信息為:

Request URL:<code>http://otherSite/shfofx/PHP/Add/addTrade</code>
Request Headers
Accept:application/json, text/plain, */*
Cache-Control:no-cache
Content-Type:application/json;charset=UTF-8
Origin:<code>http://test-shf.mysite.com</code>
Pragma:no-cache
Referer:<code>http://test-shf.mysite.com/portfolio</code>
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payload
{UserFIID:0, FinancialInstID:4, FinancialInstName:undefined, UserID:1, SHFUserID:1,…}

為什么允許印前檢查OPTIONS請求但我的POST請求被取消了?

所以答案很簡單,很煩人。

我的開發服務器配置為允許使用松散解釋的文件名,因此:

Request URL:http://otherSite/shfofx/PHP/Add/addTrade

將解析為addTrade.php,並且代碼將按預期執行。 但是,在我的測試服務器中,服務器被配置為僅返回所請求的區分大小寫的確切頁面。 因此,POST請求返回的狀態為“ 404 Not Found”。

更糟糕的是,Chrome的“網絡”標簽沒有僅傳遞“ Access-Control-Allow-Origin”錯誤的404響應。 直到我檢查了服務器響應firefox之后,我才能夠確定並解決問題。

我只是改變了:

Request URL:http://otherSite/shfofx/PHP/Add/addTrade

至:

Request URL:http://otherSite/shfofx/PHP/Add/addTrade.php

就是這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM