【问题标题】:AngularJS performs OPTIONS request instead of GET and got 405 ErrorAngularJS 执行 OPTIONS 请求而不是 GET 并得到 405 错误
【发布时间】:2016-02-26 17:30:17
【问题描述】:

我从 Angular 向 .NET Web API 应用程序发送 GET 请求,但出现 405 错误。

当我在 POSTMAN 上向相同地址发送相同请求时,一切看起来都正确。

我的 api 代码

public IHttpActionResult GetUsers()
{
    response.error = false;
    response.data = db.Users;
    response.message = result.message;
    return Ok(response);
}

我的 Angular 代码如下

service.getCategories = function (callback) {

    var url = $rootScope.servicePath + "/categories";

    $http({
        method: 'GET',
        url: url
    })
        .then(function successCallback(response) {
            callback(response.data)
        });

};

请求结果:

【问题讨论】:

标签: javascript angularjs asp.net-web-api


【解决方案1】:

当我将此行添加到控制器的头部时

[EnableCors(origins: "*", headers: "*", methods: "*")]

问题解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 2014-04-16
    • 2019-05-15
    • 2020-05-02
    相关资源
    最近更新 更多