【问题标题】:405 Method Not Allowed for URL angular2+ typescript webappURL angular 2 typescript web 应用程序不允许使用 405 方法
【发布时间】:2018-07-01 04:38:46
【问题描述】:

我正在开发一个带有打字稿的 angular2+ 应用程序。我正在尝试为我的 Angular2 Kendo 网格之一提取数据并进行以下调用以获取填充网格的结果。在这个网络电话中,我发送了一个状态 ID、skip 和 take 以及一个 Sort 数组,其中包含我要排序的字段和我希望它排序的方向。

以下是我的打字稿代码,用于调用网络服务:

  getUserRequests(statusId: number, skip: number, take: number, sort: SortDescriptor[]): Observable<GridResult> {
                     private getUserRequestsUrl = environment.serviceHostName + environment.serviceAppURL + '/api/Dashboard/GetUserRequestsByStatuses';

        var headers = new Headers();
        headers.append('Content-Type', 'application/json;');

        return this.http.post(this.getUserRequestsUrl, JSON.stringify({ "Filter": { "Field": "StatusId", "Value": statusId, "Operator": "eq" },
 "Skip": skip, "Take": take, "Sort": sort }), { headers: this.headers }).share()
            .map((res: Response) => res.json())
            .map(response => (<GridResult>{
                data: response.Data ,
                total: response.Count
            }));

    }

这是我永远不会受到攻击的服务端代码。

[HttpGet]
public HttpResponseMessage GetUserRequestsByStatuses([FromBody] DataRequest model)
{
    DataResponse<AngKendoGridDashboard> response =
        BusinessAccess.GetUserRequestsByStatuses(model);
    return CreateHttpResponse(response);
}

我面临的问题: 出于某种原因,在进行此 Web 服务调用时,我收到一条错误消息

“错误响应,状态为:405 URL 不允许方法: http://localhost/Services/RequestService/api/Dashboard/GetUserRequestsByStatuses" 我检查了正在发送的请求对象,这就是它的样子

{"Filter":{"Field":"StatusId","Value":2,"Operator":"eq"},"Skip":0,"Take":5,"Sort":[{"field":"CreatedDate","dir":"desc"}]}

在响应正文中我也看到了这条消息:

{"Message":"请求的资源不支持http方式 '发布'。"}

我从周五开始就一直在研究这个问题,但无法找到解决方案。任何帮助表示赞赏。我认为这可能与我通过打字稿发送请求对象的方式有关。

【问题讨论】:

  • POST 与 GET 不同。

标签: javascript c# angular typescript http-status-code-405


【解决方案1】:

错误表示方法 POST 不受支持,但该操作具有 HttpGet 属性。

您需要将HttpGet 更改为HttpPost

[HttpPost]
public HttpResponseMessage GetUserRequestsByStatuses([FromBody] DataRequest model)
{
    DataResponse<AngKendoGridDashboard> response = 
        BusinessAccess.GetUserRequestsByStatuses(model);
    return CreateHttpResponse(response);
}

您也可能会丢失 [FromBody] 属性,这是给定参数类型的默认值。

【讨论】:

    猜你喜欢
    • 2016-04-18
    • 1970-01-01
    • 2016-03-23
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2014-06-11
    相关资源
    最近更新 更多