我使用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