繁体   English   中英

所需的字符串参数不存在:与 fetch 通信

[英]Required String parameter is not present : communicate with fetch

我正在尝试使用 fetch API 从我的代码中删除 JQuery,但我在将数据发送到服务器时遇到了问题:

这是我的功能:

url = '/authentication'
fetch(url, {
     method: 'POST',
     headers: {"Content-Type" : "application/json"},
     body: {username: username}
});

和弹簧侧:

@PostMapping("/authentication", produces = "application/json")
@ResponseBody
public String authentication(HttpServletRequest request, @RequestParam(name = "username") String username){
...
}

但我收到以下错误:

已解决 [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'username' is not present]

使用 fetch 发送数据的正确方法是什么,我做错了什么? 我试图将JSON.stringify{"username":username}放入,但它没有改变任何东西。

谢谢你的帮助

编辑 06/24:我用url = '/authentication?username='+username解决了它,但我有一个长jsonResponse有很多字符的问题......( Request header is too large ,仅在我的手机上)

@RequestParam 将解析 url 中的 var。

url = '/authentication?username=abc'

如果你想处理 postbody

public String authentication(HttpServletRequest request, @RequestBody YourRequest request)

你的请求.java

@Data
public class YourRequest {


    private String username;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM