gpt4 book ai didi

Angular 7 HttpClient 将请求参数发送到端点的空值

转载 作者:行者123 更新时间:2023-12-01 21:57:59 24 4
gpt4 key购买 nike

我有一个使用这个 Put 请求方法的 Angular 服务:

put(peopleFieldID: number, peopleFieldName: string): Observable<number> {
let params = new HttpParams();
params = params.append("peopleFieldID", peopleFieldID.toString());
params = params.append("peopleFieldName", peopleFieldName);

return this.http
.put<number>(this.url, { params: params })
.pipe(catchError(this._errorHandling.handleError.bind(this)));}

上面的代码点击了我的 .net 核心 API 上的这个端点:

    [Authorize(Roles = "Client")]
[Route("")]
[HttpPut]
public IActionResult C_Update(int peopleFieldID, string peopleFieldName )
{
//Make call to the proper repo method.
return Json(_peopleFieldRepo.C_Update(peopleFieldID, peopleFieldName));
}

peopleFieldID 和 peopleFieldName 这两个参数始终为 0 和 null。我已经指出 Angular 前端正确发送了参数,但后端无法识别它们。我有许多其他端点,在这些端点上效果很好。

最佳答案

您需要使用 HttpParams 设置查询参数,如果您没有任何有效负载,则需要传递 null:

const params = new HttpParams()
.set('peopleFieldID', peopleFieldID.toString())
.set('peopleFieldName', peopleFieldName);
// if you have a payload to pass you can do that in place of null below
return this.http
.put<number>(this.url, null, { params: params })
.pipe(catchError(this._errorHandling.handleError.bind(this)));}

关于Angular 7 HttpClient 将请求参数发送到端点的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55389128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com