[英]Calling WCF REST service using AngularJs giving error
I am trying to call my WCF REST service(hosted in non PROD environment) from my local machine using AngularJS . 我正在尝试使用AngularJS从本地计算机调用WCF REST服务(托管在非PROD环境中)。 But everytime i am getting
但是每次我得到
SEC7119: XMLHttpRequest for http://XXX/XXX/Web/rest/GeDetails required CORS preflight. SEC7119: http:// XXX / XXX / Web / rest / GeDetails的XMLHttpRequest要求CORS预检。
and 和
SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied. SCRIPT7002:XMLHttpRequest:网络错误0x80070005,访问被拒绝。
Is there any way i can consume WCF REST service using AngularJS ? 有什么办法可以使用AngularJS使用WCF REST服务吗?
thanks! 谢谢!
You had a CORS error. 您有CORS错误。
You must enable CORS in your api -> enabling-cross-origin-requests-in-web-api 您必须在api中启用CORS-> 在web-api中启用cross-origin-requests-in
for WCF Rest try this 对于WCF Rest,请尝试一下
And then in your angular.config you must: 然后在angular.config中,您必须:
angular.module('myapp',[...])
.config(function ($stateProvider, $urlRouterProvider, $httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
// ...
})
Apparently in the latest AngularJS versions you don't have to add anything to make it work . 显然,在最新的AngularJS版本中,您无需添加任何内容即可使其工作 。 But actually for me works in this way.
但是实际上对我来说就是这样。
I hope this helps 我希望这有帮助
used below code in my service global.asax page and its working perfect. 在我的服务global.asax页面中的以下代码中使用了它,并且工作正常。
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.