gpt4 book ai didi

java - Angular 8 : Why do I receive null when using URL Query Strings?

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

我正在尝试使用以下网络服务

@POST
@Path("/delete")
@Produces(MediaType.APPLICATION_JSON)
public boolean deletePost(@QueryParam("idPost") String idPost) {
// logic here
}

但是,尝试从 Angular 为 QueryParam idPost 分配值是行不通的。 idPost 始终以 null 值到达。

尝试文档中指定的两种形式,但它对我不起作用。我做错了什么?

public deletePost(idPost: number): Observable<any> {
const options = idPost ?
{ params: new HttpParams().set('idPost', idPost.toString()) } : {};
return this.httpClient.post('url-delete',options);
}

public deletePost(idPost: number): Observable<any> {
const params = new HttpParams({fromString: 'idPost=' + idPost.toString()});
return this.httpClient.post('url-delete',params);
}

最佳答案

post 中的第二个参数是正文,选项应该是一个 json,如下所示:

const options = { params: new HttpParams().set('name', 'xxx') }

现在像这样调用它:

public deletePost(idPost: number): Observable<any> {
const options = {params: new HttpParams({fromString: 'idPost=' + idPost.toString()})};
return this.httpClient.post('url-delete',{}, options); // empty body
}

关于java - Angular 8 : Why do I receive null when using URL Query Strings?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58918406/

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