我有一个奇怪的情况,可能是因为我错过了一些我没有意识到或不知道的事情。我正在使用 Angular 创建一个简单的登录 UI,并调用在 java 中创建的 Web API。java web API函数如下@RequestMapping(value = "/logon", method = RequestMethod.POST, produces = {"application/json"})@ResponseBodypublic String logon( @RequestParam(value = "userID", required = true) String userID, @RequestParam(value = "password", required = true) String password, HttpServletRequest request)现在,如果我使用 http.post 如下login(username: string, password: string) { return this.http.post(this.url+"/security/logon/", JSON.stringify({ userID: username, password: password }) )然后我在 Google Chrome 浏览器中收到以下错误:POST http://localhost:8080/logon/ 400 (Required String parameter 'userID' is not present)但是,如果我将代码更改如下:login(username: string, password: string) { var usrpwd = "userID=" + username + "&password=" + password; return this.http.post(this.url+"/security/logon?"+usrpwd, usrpwd )它完美地工作。我错过了什么吗?为什么应该是传递的参数的 http.post 的第二个参数似乎不起作用?提前感谢您的任何回复或反馈。
2 回答
千巷猫影
TA贡献1829条经验 获得超7个赞
您正在使用两个必需参数定义端点 url,并且此类参数必须在 url 中(请在此处查看),因此当您向端点发出请求时,该 url 必须为:
http://localhost:8080/logon?userID=yourUserId&password=yourUserPassword
在第一个实现中,您没有向 url 添加查询参数,因此向 url http://localhost:8080/logon/发出请求,因为它没有所需的参数,您的 web 层返回400 http code,这意味着一个错误的请求(因为再次,您的 url 不包含所需的参数)。
添加回答
举报
0/150
提交
取消