2016-10-10 22 views
0

使用WCF REST服务,我一直在关注这个实例一个Cors问题:通过JQuery

Create and Consume WCF Restful Service Using jQuery

我收到以下错误:

的XMLHttpRequest无法加载http://localhost:48839/EmployeeService.svc/GetEmployeeDetails/。对预检请求的响应不会通过访问控制检查:请求的资源上不存在“访问控制 - 允许来源”标头。因此不允许访问原产地'http://localhost:57402'。响应有HTTP状态代码404

enter image description here

回答

0

图莎尔@,

快速谷歌搜索给了我这个: Enabling CORS header

您的来电是成功的,你可能有举办服务和你的html页面在同一个域中。您正在通过端口57402从localhost获取您的html页面,并且您正试图访问端口48839上的serice,因此您会看到此CORS问题。

您必须同时托管服务终点和来自同一个域和端口的html页面,或者您必须将服务器上的访问标题列入白名单,如上述链接中所述。

+0

谢谢,我也做了同样的。但我无法弄清楚在哪里添加标题 –

+1

正如上面的链接中提到的,Application_BeginRequest方法将会是一个你可以添加的地方。 – Sreekanth

0

添加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(); 
      } 
     }