ajax请求@RequestBody问题

fyh 2022年06月29日 108次浏览
@PostMapping("/user-tokens")
public JsonResponse<String> login(@RequestBody User user)

该接口使用postman传入带有JSON的Body可以请求成功,但在前端ajax请求时候报了 SON parse error: syntax error,expect start with { or [,but actually start with error; nested exception is com.alibaba.fastjson.JSONException: syntax error,expect start with { or [,but actually start with error] 的错误。
原因在于没有指定ajax参数类型
需要在ajax请求时候指定:

contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({

}),

其中JSON.stringify中传入参数。