【发布时间】:2015-08-23 13:52:33
【问题描述】:
我将客户端托管在 localhost:8080/ 上,服务器托管在 localhost:44302/ 上
我正在尝试链接到我的后端,但遇到了 CORS 问题。下面是我的 Angular http 请求
$http.post(url, data, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS'
}
}).success(function (response) {
// rest of the code
}).error(function (err, status) {
// rest of the code
});
在用 C# 编写的服务器端,我在响应中设置了以下内容
Response.ContentType = "application/json";
Response.AddHeader("Access-Control-Allow-Origin", "*");
Response.AddHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
即使设置了这些,我也收到以下错误
XMLHttpRequest 无法加载 https://localhost:44302/some_uri。 请求中不存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:8080” 使用权。响应的 HTTP 状态代码为 400。
我错过了什么?
【问题讨论】:
-
你用什么做后端? ASP.NET Web API?你把修改响应的代码放在哪里了?
-
是的,我正在使用 ASP.NET MVC,我将代码放在控制器中,从那里我将响应发回
标签: javascript c# angularjs cors