无法使用ajax [400(错误请求)]向服务器发送信息

时间:2016-04-08 14:01:32

标签: java jquery ajax spring-mvc

我无法使用jsonJava server对象发送到ajax。浏览器控制台抛出错误:

  

获取http:// localhost:8080 / MySpring /更改? {%22name%22:%22d%22} 400(错误请求)。

我使用SpringMVC

HTML文件

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script
    src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
        $(document).ready(function() {

            $("#load").bind("click", function() {
                 var inputText = $("#mytext").val();
                 var task = {name : inputText};
                 $.ajax({
                    url : "change",
                    crossDomain: true,
                    type: 'GET',
                    dataType: 'json',
                    contentType: 'application/json',
                    mimeType: 'application/json',
                    data: JSON.stringify(task),
                    success: function (response) {
                        var result = response.name;
                        $("#result_text").text(result);
                    }
                 });    
            });

        });


</script>
</head>
<body>
    <input type="text" name="mytext" id="mytext">
    </br>
    <input type="button" name="load" id="load" value="Load">
    </br>
    <p id="result_text"></p>
</body>
</html>
@Controller
public class MailController {

  @RequestMapping(value = "/change", method = RequestMethod.GET)
  public @ResponseBody 
  Task change(@RequestParam String name) {
    Task result = new Task();
    result.setName(name);
    return result;
  }


}

3 个答案:

答案 0 :(得分:0)

试试这个。而不是动态添加json只需硬编码就像{“name”:“test”}

$.ajax({
            type: "GET",
            url: "HERE PUT THE PATH OF YOUR SERVICE OR PAGE",
            data: '{"HERE YOU CAN PUT DATA TO PASS AT THE SERVICE"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
               //do something
            },
            error: function (errormessage) {

                //do something else

            }
        });

答案 1 :(得分:0)

控制器映射存在问题。 您的控制器期待:

http://localhost:8080/MySpring/change?name=yourData

但你正在打电话

http://localhost:8080/MySpring/change?yourData

还要考虑根据http get length limit将方法从GET更改为POST。

答案 2 :(得分:0)

    <script>
            $(document).ready(function() {
                $("#load").bind("click", function() {
                     var inputText = $("#mytext").val();
                     var task = {name : inputText};
                     $.ajax({
                        url : "change",
                        crossDomain: true,
                        type: 'GET',
                        dataType: 'json',
                        contentType: 'application/json',
                        mimeType: 'application/json',
                        data: JSON.stringify(task),
                        traditional : true,
                        success: function (response) {
                            var result = response.name;
                            $("#result_text").text(result);
                        }
                     });    
                });
            });
    </script>


Added traditional : true, in Ajax call