我triyng做一個簡單的事情,與ajax,發送請求(使用GET或POST)。如何在春季發送和檢索參數?
我將以json格式發送2個參數,我只是想讓他們回來併發送響應,但是,我總是得到一個錯誤400和其他人,我不知道最新錯誤,任何想法如何?
我開始根據這篇文章:http://fruzenshtein.com/spring-mvc-ajax-jquery/
我使用Spring MVC的。
到目前爲止,我有這樣的:
$(".update_agent").live('click', function(){
var agent = { "agentId" : agentID, "hostAGent" : hostID};
//send ajax
$.ajax({
url: url,
data: JSON.stringify(agent),
type: "GET",
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(data) {
alert("success");
},
error: function(){
alert("error");
}
});
})
,並在我的Java控制器我有這個
@RequestMapping(value = "/update", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public int updateAgent(HttpServletRequest req, HttpServletResponse res) throws IOException{
req.getParameterValues("agentId");
return AGENT_UPDATE_SUCCESS;
}
,但我不能拿回來,不知道如何使則params的要求, 任何想法?
謝謝。
=====================更新======================== ====
伊夫改變了代碼,這是怎麼看起來像......
$.ajax({
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
type: 'POST',
url: url,
data: JSON.stringify(agent),
dataType: 'json',
success:function(data) {
alert("success");
},
error: function(){
alert("error");
}
});
而在我的控制器
@RequestMapping(value = "/update", method = RequestMethod.POST)
public @ResponseBody Integer updateAgent(@RequestBody String param) throws IOException{
System.out.println(param);
//do something...
return 1;
}
的問題是,我得到一個錯誤415,不受支持的媒體類型,有何建議?
你在哪裏定義網址? – Muraad
之前的點擊...與其他常數那裏相同... – jpganz18