一衷掷、什么時(shí)候需要用qs進(jìn)行序列化:
axios默認(rèn)的content-type是application/json,即json格式蜜猾,后臺(tái)可以使用字符串進(jìn)行接收秀菱,然后再解析即可:
默認(rèn)(不使用qs):
發(fā)送請(qǐng)求:
export const getOutList = params => {
return service.post(launchReciveApiConfig.getOutList, params);
}
后臺(tái)接收:使用字符串接收后,通過json方法獲取需要數(shù)據(jù)即可
@PostMapping("/getOutList")
@ResponseBody
public AjaxResult getOutList(@RequestBody String params) {
Map map = JSONObject.parseObject(params);
String name = map.get("name").toString();
//...
}
傳遞的數(shù)據(jù)格式如下(json格式):
{
name:xxx,
age:xxx
}
使用qs.stringify解析后蹭睡,content-type是application/x-www-form-urlencoded,即form表單格式衍菱,后臺(tái)可以使用實(shí)體進(jìn)行接收:
使用qs:將數(shù)據(jù)序列化成url格式
發(fā)送請(qǐng)求:
export const editOut = params => {
return service.post(launchReciveApiConfig.editOut, qs.stringify(params));
}
后臺(tái)接收:使用TOut實(shí)體類接收即可
@PostMapping("/editOut")
@ResponseBody
public AjaxResult editOut(TOut tOut) {
//...
}
傳遞的數(shù)據(jù)格式如下(form表單格式):
name:xxx&age:xxx
所以,實(shí)際上是否需要用qs去序列化參數(shù)完全取決于后端要怎么接受數(shù)據(jù)。
二肩豁、qs.parse方法:
qs.parse()將URL解析成對(duì)象的形式脊串;
let url = 'name:xxx&age:xxx'
console.log(qs.parse(url));
輸出:
{
name:xxx,
age:xxx
}
三、qs處理數(shù)組及springBoot后臺(tái)接收
參考:https://blog.csdn.net/pifutan/article/details/86320705