我正在使用 Vue.js、axios 和 Spring。
在頁面上我有以下 axios 代碼
axios({
method: 'post',
url: '/user/info',
params: {
'_csrf' : document.getElementById('csrf_id').value,
'name' : 'job',
'age' : '25',
},
headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'}
});
在服務器上我有一個這樣的接收方法
@Controller
@RequestMapping("/user")
public class UserInfo {
@ResponseBody
@PostMapping(value = "/info", consumes = "application/x-www-form-urlencoded", produces = "application/json" ";charset=utf8")
public String info(@RequestParam(value = "name") String name, @RequestParam(value = "age") String age) {
System.out.println(name);
System.out.println(age);
return "ok";
}
}
Axios 向服務器發出請求,但服務器回傳 415 回應。請求標頭缺少application/x-www-form-urlencoded內容型別。我懷疑問題正是在于此。
告訴我,我做錯了什么?
uj5u.com熱心網友回復:
HttpMethod Post 是一種在請求體中寫入和傳輸資料的方法。
在您的情況下,您將資料通過params. 如果您在撰寫代碼時執行代碼,資料將被發送,/user/info?_csrf=value&name=job&age=25并且請求正文中將沒有資料。
為了得到你想要的回應,你可以修改它如下。
axios({
method: 'post',
url: '/user/info',
data: '_csrf=csrf&name=job&age=25',
headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'}
});
將params關鍵字更改為data并寫入查詢字串等資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/321966.html
