我使用基於Jquery的Jstree庫捕獲分層數據。數據是JSON格式,我想捕獲並將此數據綁定到我的bean類JstreeJson.java)。這裏是我到目前爲止已經試過..獲取「錯誤不支持的媒體類型」,同時綁定彈簧mvc控制器的JSON數據
Ajax調用:
function getJSON() {
var jstree = $('#jstree1').jstree(true).get_json('#', {flat:true});
console.log(JSON.stringify(jstree));
$.ajax({
type:"POST",
url:"createObjective",
data : { jstree: jstree },
dataType :"json",
success : function(result) {
console.log(jstree);
console.log(result);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus, errorThrown);
}
});
}
的console.log輸出:
[{"id":"j1_1","text":"Simple root node","icon":true,"li_attr":{"id":"j1_1"},"a_attr":{"href":"#","id":"j1_1_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"#"}]
控制器
@RequestMapping(value="/createObjective",method=RequestMethod.POST)
public @ResponseBody String createObjective(@RequestBody JstreeJson jstree)
{
System.out.println(jstree);
return "done";
}
Bean類
public class JstreeJson
{
private String id;
private String text;
private String parent;
// getters and setter
}
我曾嘗試加入消耗和接頭,但它沒有對我的輸出
@RequestMapping(value="/createObjective",method=RequestMethod.POST,consumes="application/json",headers = "content-type=application/x-www-form-urlencoded")
在@RequestMapping上使用'headers =「Accept = application/json」'並移除consumes屬性。 – Darshan
沒有工作得到相同的錯誤 – piechuckerr