我使用ember.js並拋出我這個錯誤:訪問控制允許來源錯誤ember.js
XMLHttpRequest cannot load http://127.0.0.1:20000/accounts/0 . Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:4200 ' is therefore not allowed access.
在我的服務器端,我把代碼打印出記錄GET,POST和PUT調用。它不打印任何東西。因此,我認爲客戶端從未向服務器提出任何請求。 (但是,訪問'http://127.0.0.1:20000/accounts/0'手動確實調用了GET方法的日誌。)
服務器端已編碼爲返回Access-Control-Allow-Origin,並且它適用於其他方法。
那麼,我的問題是什麼?
,只爲記錄,在Java服務器端是:
responseBuilder.header("Access-Control-Allow-Origin", "http://localhost:4200");
responseBuilder.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization, auth-token");
responseBuilder.header("Access-Control-Allow-Credentials", "true");
responseBuilder.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
responseBuilder.allow("OPTIONS");
,並在我的ember.js配置/ environment.js我:
ENV['contentSecurityPolicy'] = {
'default-src': "*"
};
預檢意味着「OPTIONS」請求。 – CBroe
允許OPTION請求 –
端口是否匹配?不同的端口=不同的域,所以http:// localhost:4200與http://127.0.0.1:20000不一樣的域 – Shilly