桃花長(zhǎng)相依
2021-09-15 16:52:10
我有一個(gè)奇怪的情況,可能是因?yàn)槲义e(cuò)過(guò)了一些我沒(méi)有意識(shí)到或不知道的事情。我正在使用 Angular 創(chuàng)建一個(gè)簡(jiǎn)單的登錄 UI,并調(diào)用在 java 中創(chuàng)建的 Web API。java web API函數(shù)如下@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)現(xiàn)在,如果我使用 http.post 如下login(username: string, password: string) { return this.http.post(this.url+"/security/logon/", JSON.stringify({ userID: username, password: password }) )然后我在 Google Chrome 瀏覽器中收到以下錯(cuò)誤: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 )它完美地工作。我錯(cuò)過(guò)了什么嗎?為什么應(yīng)該是傳遞的參數(shù)的 http.post 的第二個(gè)參數(shù)似乎不起作用?提前感謝您的任何回復(fù)或反饋。
2 回答

千巷貓影
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
您正在使用兩個(gè)必需參數(shù)定義端點(diǎn) url,并且此類(lèi)參數(shù)必須在 url 中(請(qǐng)在此處查看),因此當(dāng)您向端點(diǎn)發(fā)出請(qǐng)求時(shí),該 url 必須為:
http://localhost:8080/logon?userID=yourUserId&password=yourUserPassword
在第一個(gè)實(shí)現(xiàn)中,您沒(méi)有向 url 添加查詢參數(shù),因此向 url http://localhost:8080/logon/發(fā)出請(qǐng)求,因?yàn)樗鼪](méi)有所需的參數(shù),您的 web 層返回400 http code,這意味著一個(gè)錯(cuò)誤的請(qǐng)求(因?yàn)樵俅?,您?url 不包含所需的參數(shù))。
添加回答
舉報(bào)
0/150
提交
取消