XMLHttpRequest无法加载http:// localhost:57997 / Home / Get

时间:2016-12-31 11:43:20

标签: angularjs asp.net-mvc-4 asp.net-web-api

当我尝试从MVC访问WebApi时,我收到此错误

XMLHttpRequest cannot load http://localhost:57997/Home/Get. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:64035' is therefore not allowed acces

Service.Js

app.service('MyService', function ($http) {

  var ApiAddress = "http://localhost:57997/";

  this.GetWebData = function () {
    var Getdatas= $http({
      url: ApiAddress + 'Home/Get',
      type: 'GET',
      dataType: 'json',
      params: JSON.stringify(),
      content:{'content-type' :'application/Json'}
    })
    return Getdatas;
  }
});

Controller.Js

app.controller('WebCtrls', function ($scope,MyService) {
  $scope.Hello = "Hello angular How r u...";

  $scope.GetDb = function () {
    alert('Hello..');
    var SerData = MyService.GetWebData();
    SerData.then(function (d) {
      $scope.Emp = d.data;
    })
  }
})

的WebAPI

public JsonResult Get()
{
    var x = prod.GetEmployees();
    return new JsonResult { Data = x, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

Global.asax中

在WebApi全局文件中,我为跨页原点编写了以下代码

protected void Application_BeginRequest()
{
    string[] allowedOrigin = new string[] { "http://localhost:57997/" };
    var origin = HttpContext.Current.Request.Headers["Origin"];
    if (origin != null && allowedOrigin.Contains(origin))
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", origin);
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST");
        //Need to add more later , will see when required
    }
}

1 个答案:

答案 0 :(得分:0)

您可以通过从chrome.exe所在的文件夹位置执行以下命令来禁用Chrome浏览器的Web安全选项来处理它。首先关闭所有chrome实例。然后运行以下命令

  

chrome.exe --disable-web-security

您可以在服务器端处理它,同时过滤所有来自服务器的请求,将标题添加到响应中。

My function looks like that:
Public Function CountCardsinList(ListId As String) As Integer
    WebHelpers.EnableLogging = False


    Dim TrelloClient As New WebClient
    TrelloClient.BaseUrl = "https://api.trello.com/1/"

    Dim Request As New WebRequest
    Request.Format = WebFormat.Json
    Request.ResponseFormat = Json

    Request.Resource = "lists/{ListId}/cards"

    Request.AddUrlSegment "ListId", ListId

    Request.AddQuerystringParam "key", TrelloAPIKey
    Request.AddQuerystringParam "token", TrelloAPIToken
    Request.AddQuerystringParam "filter", "open"


    Dim Response As WebResponse
    Set Response = TrelloClient.Execute(Request)

    If Response.StatusCode = WebStatusCode.Ok Then
    Debug.Print Response.Content        '


    'Response.Data("idList").Count

        Debug.Print "CountCardsinList =>>> " & Response.Content
        CountCardsinList = Response.Data("idList").Count

    Else
        MsgBox Response.StatusDescription, vbCritical, "Error " &                   Response.StatusCode
        CountCardsinList = ""

    End If

    Debug.Print "CountCardsinList =>>> " & Response.Content
    'Set CountCardsinList = Request


End Function