前端代码如下
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded'
})
}
const params = {
"type": "phone",
"key": "1234567"
}
this.http.post('/bss/verifyCode', params , httpOptions).subscribe();
后端代码如下
@PostMapping
public ReturnMsg sendVerifyCode(String type, String key) {
if (PHONE.equals(type)) {
return service.sendVerifyCodeByPhone(key);
} else {
return service.sendVerifyCodeByEmail(key);
}
}
使用resttemplate或Postman构造post请求测试接口均可以正常使用,但是在前端angular环境中调用接口就无法接收到参数,请求正确接受到了,但是没有参数。另外改用url传参也是可以接收到的。
前端代码发送的请求内容如下
请各位前后端大神指点,这是哪里出了问题
问题已解决,前后端预期接收参数的Content-Type要一致才可以。@PostMapping默认不是x-www-form-urlencoded