2014-10-02 43 views
6

我試圖用ajax發送請求,但有狀態400錯誤請求。 我應該發送什麼樣的數據以及如何在控制器中獲取數據? 我敢肯定,要求是精唯一參數出錯如何在Spring框架中使用參數發送和接收ajax請求?

JSP

<script type="text/javascript"> 

    var SubmitRequest = function(){ 
     $.ajax({ 
       url : "submit.htm", 
       data: document.getElementById('inputUrl'), 
       type: "POST", 
       dataType: "text", 
       contentType: false, 
       processData: false, 
       success : 
        function(response) { 
         $('#response').html(response); 
        } 
     }); 
    } 
    </script> 

控制器

@RequestMapping(value = "/submit", method = RequestMethod.POST) 
public @ResponseBody 
String Submit(@RequestParam String request) { 
    APIConnection connect = new APIConnection(); 
    String resp = ""; 
    try { 
     resp = "<textarea rows='10'cols='100'>" + connect.doConnection(request) + "</textarea>"; 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     resp = "<textarea rows='10'cols='100'>" + "Request failed, please try again." + "</textarea>"; 
    } 
    return resp; 
} 

回答

25

要發送一個Ajax請求後,您可以使用此:

$.ajax({ 
    type: "POST", 
    url: "submit.htm", 
    data: { name: "John", location: "Boston" } // parameters 
}) 

而在Spring MVC中控制器:

@RequestMapping(value = "/submit.htm", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE) 
public @ResponseBody 
String Submit(@RequestParam("name") String name,@RequestParam("location") String location) { 
    // your logic here 
    return resp; 
} 
+0

謝謝,它的工作原理! – Bboy820602 2014-10-02 09:02:16

+0

不客氣,我很樂意爲您提供幫助 – Pracede 2014-10-02 10:02:11

相關問題