Angular httpclient post 发送表单数据

Angular表单数据

Angular post 请求默认使用的是JSON格式的请求,如果想执行form表单请求需要做简单的设置

代码示例

Angular 代码:

1
2
3
4
5
6
7
8
const formData = new URLSearchParams();
formData.set('name' , 'zhangsan');
formData.set('age', '25');
this.httpClient.post('/userservice/user', formData.toString() , {
headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
}).subscribe(
data => {}
);

后台使用Jersey接收请求

1
2
3
4
5
6
@POST
@Path("user")
@Produces({"application/json"})
@Consumes("application/x-www-form-urlencoded")
public User createUser(@FormParam("name") String name, @FormParam("agen") int age) throws ServerException {
}