2015-07-20 60 views
0

我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,不受支持的媒體類型,有何建議?

+0

你在哪裏定義網址? – Muraad

+0

之前的點擊...與其他常數那裏相同... – jpganz18

回答

1

GET請求不能有'數據'字段。你需要把你的數據作爲URL的一部分:

$.ajax({ 
     url: url + "?agent=" + 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"); 
     } 
    }); 

現在你可以在你的控制器得到的數據爲:

@ResponseBody public ResponseEntity<String> updateAgent(@RequestParam(value = "agent") String agentJson){ 
... 
} 

,或者你可以發送POST請求。用POST請求,你可以把你的數據作爲requestBody

public @ResponseBody ResponseEntity<String> updateAgent(@RequestBody String agentJson){ 
... 
} 

編輯: 創建一個新的Agent -class:

public class Agent { 
    private long agentId; 
    private long hostAgent; 
    ... 
    getter and setter 
    ... 
} 

現在控制器更新爲:

public @ResponseBody ResponseEntity<String> updateAgent(@RequestBody Agent agent){ 
    System.out.println(agent.getAgentId()); 
} 

並將ajax-Call的「Content-Type」更改爲「application/json」。

EDIT2: 你的Ajax調用數據更改爲:

data: { agentId: agentID, hostAgent : hostAgentID} , 

甚至

data: agent , 

不要忘記改變你的代理對象 「hostAGent」 到 「hostAgent」或者你會得到400!

現在AJAX將數據發送請求參數,你可以得到你的控制器中的數據:

public @ResponseBody ResponseEntity<String> updateAgent(@RequestParam(value = "agentId") long agentId, @RequestParam(value = "hostAgent") long hostAgentId){ 
    System.out.println(agentId); 
} 
+0

當我發佈帖子請求我總是得到狀態415,不支持的媒體類型...任何想法? – jpganz18

+0

只是在您的控制器 – JohnnyAW

+0

btw中刪除'consumes = MediaType.APPLICATION_JSON_VALUE'。我將刪除'beforeSend'並將其替換爲header-property:'headers:{ Accept:「application/json」, 「Content-Type」:「application/json」 }' – JohnnyAW