前端
function deleteTool () {
var url = 'http://localhost:8080/admin/deleteTool';//传值的地址
var data = { toolnum: '0' };//传值的内容,键:值格式
fetch(url, {
method: 'post',//post方法
headers: {
"Content-Type": "application/json;charset=utf-8",
'Token': localStorage.getItem('token')
},
body: JSON.stringify(data)//转为json格式
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', response));
console.log('--------'+data);
}
后端
@RequestMapping(value="/admin/deleteTool",method = RequestMethod.POST,produces = "application/json")
public int toDeleteTool(@RequestBody String toolnum) throws JSONException {
JSONObject json =new JSONObject(toolnum);//解析接受到的json数据
System.out.println("..........."+json.getString("toolnum"));
return toolDaoImpl.deleteTool(json.getString("toolnum"));
}
- 必须在参数toolnum前加上
@RequestBody
,不然接受到的值为null。 - json.getString(“toolnum”)为获取json中的值,详情参考java json数据取值