C# Angularjs No';访问控制允许原点';请求的资源上存在标头
我收到的错误信息如下: 加载失败:请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。响应的HTTP状态代码为404 甚至我的WebAPi(Global.asax.cs)文件中也有Access ControlOrion文件 Angular.jsC# Angularjs No';访问控制允许原点';请求的资源上存在标头,c#,angularjs,json,C#,Angularjs,Json,我收到的错误信息如下: 加载失败:请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。响应的HTTP状态代码为404 甚至我的WebAPi(Global.asax.cs)文件中也有Access ControlOrion文件 Angular.js $scope.SubmitForm = function (isValied) { var EmployeeDetails = { 'Empname': $sc
$scope.SubmitForm = function (isValied) {
var EmployeeDetails = {
'Empname': $scope.Emp.EmpName
}
if (isValied) {
EmployeeFactoryService.SaveEmployeee(EmployeeDetails).then(function ()
})
}
}
EmployeeFactoryServices.SaveEmployeee = function (Employee) {
return $http({
url: 'http://localhost:10948/Api/Home/PostData/'+ Employee,
method: 'POST',
ContentType: 'application/x-www-form-urlencoded', })
Factory.Js
$scope.SubmitForm = function (isValied) {
var EmployeeDetails = {
'Empname': $scope.Emp.EmpName
}
if (isValied) {
EmployeeFactoryService.SaveEmployeee(EmployeeDetails).then(function ()
})
}
}
EmployeeFactoryServices.SaveEmployeee = function (Employee) {
return $http({
url: 'http://localhost:10948/Api/Home/PostData/'+ Employee,
method: 'POST',
ContentType: 'application/x-www-form-urlencoded', })
有一个chrome扩展cors。
使用它,您就可以开始了。1-从您的URL中可以看出,您试图发送的对象没有字符串化,因此我建议使用
JSON.stringify(EmployeeDetails)
2-对于post请求,您最好使用内容类型:application/json
3-您必须允许客户端应用程序的请求进入后端代码
4-您可以通过以下方式更改配置CORS的方式来暂时解决此问题:
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
//These headers are handling the "pre-flight" OPTIONS call sent by the browser
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
也应该是,
return $http({
url: 'http://localhost:10948/Api/Home/PostData/'+ ,
method: 'POST',
data : Employee,
ContentType: 'application/json' })
可能重复的不必担心源标题,只需将标题设置为所有允许的源,浏览器将处理其余的;)您还需要确保在路由中处理选项飞行前请求