虽然在angularjs中使用web api会返回404错误

时间:2016-01-19 14:12:19

标签: angularjs asp.net-web-api

这是我的代码

$http.jsonp("http://admin.mlbmcjhs.com/api/Department?&callback=JSON_CALLBACK").success(function (data, status) {
    $scope.Departments = data;
    alert(data);
}).error(function (data,status) {
    alert(status);
});

web api在浏览器控制台中正常运行 并返回数据

但是在angularjs中它会跳转到error()并返回404

1 个答案:

答案 0 :(得分:0)

您可能收到了CORS错误。我已经从Plunker(http://plnkr.co/edit/q3DVia?p=info

进行了测试
  var url = "http://admin.mlbmcjhs.com/api/Department?&callback=JSON_CALLBACK";
  $http({
    url: url,
    method: "GET",
    headers: {
        'Content-Type': 'application/JSON'
    },
}).success(function(response, status, headers, config, statusText) {
    vm.Departments = data;
    vm.status = status;
    vm.headers = headers();
}).error(function(response, status, headers, config, statusText) {
    vm.status = status;
    vm.headers = headers();
});

,我收到一个CORS错误。您可能需要在webApi上启用CORS。