angular - Angular 7 HttpClient 将请求参数发送到端点
问题描述
我有一个带有这个 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 - 在 Angular 11 材质 UI 中,我收到错误 TS2531: Object is possible 'null' in my html template
- java - Java,将字节数组转换为 HashMap 或 JSON 字符串
- java - RXJava 顺序执行更多的 observable
- google-cloud-platform - 是否可以为 Google Cloud Run 禁用 Stackdriver 日志记录?
- google-apps-script - 如何排除日本银行假日并仅在 Google Apps 脚本中的日期中添加工作日?
- c++ - CreateProcessA() 在 C++ 中使用 cmd 输出以管理员身份运行
- python - Python Json 微笑序列化包
- python - 使用 BeautifulSoup 提取元素中的文本
- sql-server - SQL Server 错误:字符串或二进制文件将被截断。(系统.数据)
- node.js - 我的快递服务器收到很多获取请求