後臺用Map<String,String>接:
@RequestMapping("/save")
public void saveFile(@RequestBody Map<String,String> map){
//map.get("path")
//map.get("content")
}
前臺:
var data={"content":"我是數據","path":"我是路徑"};
$.ajax({
url: "/tree/save",
type: "post",
contentType: "application/json",
dataType: "json",
data:JSON.stringify(data) ////將JSON對象轉化爲JSON字符串
});
說明:RequestBody接收json字符串。而ajax傳的是json對象。
需要用JSON.stringify(data)把對象轉爲json字符串。
JSON字符串
var data='{"content":"我是數據","path":"我是路徑"}';
Json對象
var data={"content":"我是數據","path":"我是路徑"};