chrome和mozilla中的Angular js报告错误

时间:2015-07-21 07:25:01

标签: javascript jquery asp.net angularjs wcf

我是Angular Js的新手。今天在编写代码时遇到了这个错误 XMLHttpRequest无法加载http://localhost:8090/TimeSheetLog/ValidateUser/we/we。 No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' http://localhost:55273'因此不允许访问。 enter image description here

1http://i.stack.imgur.com/OuHTV.png WebConfig Fileimage

enter image description here控制器代码

在此我尝试在My Controller Code As中访问WCF服务。

1 个答案:

答案 0 :(得分:0)

所以问题不在于你的AngularJS代码,而是在服务器上。您使用什么作为您的Web服务器?我看到你的问题中有asp.net标记,所以如果是这种情况,你可以在你的处理程序中添加:

public void ProcessRequest(HttpContext context)
{
  context.Response.AppendHeader("Access-Control-Allow-Origin", "*"); 
}

这应该允许你做CORS,这是你需要的。

您还需要修改web.config以包含:

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
    <handlers>
      <add name="ExampleHandler" verb="*" path="ExampleHandler.ashx" type="ExampleNamespace.ExampleHandler, ExampleNamespace, Version=1.0.0.0, Culture=neutral" />
    </handlers>
  </system.webServer>