如何在Spring Framework中使用参数发送和接收ajax请求?

Bbo*_*602 11 javascript ajax jquery spring spring-mvc

我尝试使用ajax发送请求,但状态为400错误请求.我应该发送什么样的数据以及如何在控制器中获取数据?我确定请求很好,只有参数出错

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>
Run Code Online (Sandbox Code Playgroud)

调节器

@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;
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*ede 32

要发送Ajax发布请求,您可以使用:

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

在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;
}
Run Code Online (Sandbox Code Playgroud)