我试图以这种方式发布到WebApi后端:
'use strict';
angular.module('auth', []).factory('authService', ['$http', '$q', '$localStorage', function ($http, $q, $localStorage) {
var serviceBase = 'http://localhost:53432/api/';
var authServiceFactory = {};
var authentication = {
isAuth: false,
userName: ""
};
var saveRegistration = function (registration) {
return $http.post(serviceBase + 'account/register', registration).then(function (response) {
return response;
});
};
但我收到错误"message":"The origin 'http://evil.com/' is not allowed."
我理解它与CORS问题有关,所以我在模块$sceDelegateProvider
中定义:
$sceDelegateProvider.resourceUrlWhitelist([
'self',
'http://localhost:53432' //This is webapi url
]);
但这并没有帮助。我该怎么解决这个问题?
在服务器端已启用CORS:
[AllowAnonymous]
[Route("Register")]
[HttpPost]
[EnableCors(origins: "http://localhost:8080", headers: "*", methods: "*")]
public async Task<IHttpActionResult> Register(RegisterBindingModel register)
答案 0 :(得分:8)
这很有趣,但问题是通过关闭浏览器中的CORS插件解决的。
解决问题,由andrey.shedko撰写。
答案 1 :(得分:2)
刚遇到这个问题
您正在使用使用evil.com发出请求的扩展程序,因此&#39; localhost:....&#39;从来没有工作,因为&#39;起源&#39;实际上并不是本地主机:...&#39;。
关闭插件,它会起作用。
希望它有所帮助。
答案 2 :(得分:1)
Access-Control-Allow-Origin
设置在来自服务器的响应上,而不是客户端请求上,以允许来自不同来源的客户端访问响应。
您的JavaScript无法授予自己访问其他网站的权限。
如果您使用webapi,请阅读此内容以及如何为其他网站请求启用CORS。 https://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api
我认为这将对你有所帮助。
答案 3 :(得分:0)
为了补充@Thomas Pessato,在 Angular 的情况下,我使用了名为“ Allow-Control-Allow-Origin ”的插件作为Chrome扩展程序。这是在添加“原始http://evil.com”。 关闭它可以解决问题。