我在使用AngularJS的服务器上启用CORS时遇到问题.我正在使用Angular 1.2.16,这是我的服务器配置:
Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Headers "Content-Type, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name, Authorization" Header set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS" Header set Access-Control-Allow-Credentials "true"
我可以使用以下请求:
$http.post(configuration.authUrl, {username: 'username', password: 'password'}) .success(function (data) { $cookieStore.put(configuration.sessionName, { token: data.authenticationToken, user: data.user }); }) .error(function () {}));
因为此请求不使用自定义标头.
当我之后尝试请求以下内容时:
Balance.get()
余额为:
angular.module('portalApp') .factory('Balance', ['$resource', 'Auth', 'configuration', function ($resource, Auth, configuration) { return $resource(configuration.balanceUrl, {}, { get: { method: 'GET', isArray: false, headers: { Authorization: Auth.getAuthToken() } } }); }]);
我得到了一个401 Unauthorized
关于balanceUrl的东西.
在我配置的配置中:
$httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With'];
我甚至尝试$http.defaults.headers.common.Authorization = Auth.getAuthToken();
过$resource
在Balance
资源工厂之前,但这没有帮助.
无论我使用什么方法,发送到预检OPTIONS
请求的Authorization
标头都没有标头.这些是预检OPTIONS
请求的请求标头.
OPTIONS /api/v1.0/user/orders HTTP/1.1 Host: host Connection: keep-alive Cache-Control: no-cache Access-Control-Request-Method: GET Pragma: no-cache Origin: origin User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 Access-Control-Request-Headers: accept, authorization Accept: */* Referer: referer Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8
有什么建议?