415使用ajax调用控制器时出错

时间:2015-05-21 05:05:18

标签: java jquery ajax spring-mvc model-view-controller

这是我的jsp页面home.jp

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('#sampleForm').click(
        function(event) {

            var firstname = "Chethan";
            var age = 12;

            var json = {
                    "name":firstname,
                    "age":age
            }
            alert(json)
            $.ajax({
                url : "/com/testAjacCall",

                 data: JSON.stringify(json),
                type : "POST",
                mimeType: 'application/json',
                beforeSend: function(xhr) {  
                    xhr.setRequestHeader("Accept", "application/json");  
                    xhr.setRequestHeader("Content-Type", "application/json");  
                },  
                success : function(response) {
                    alert("SUCCESS--->"+ response );
                },
                error : function(error) {
                    alert("error---->"+error);
                }
            });
            return false;
        });
    });
</script>

这是我的控制器HomeController.java

      @RequestMapping(value = "/testAjacCall", method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String processAJAXRequest(@RequestBody Person person   ,HttpServletResponse response) {
        String jQresponse = "Hello";
        System.out.println("IAMWORKING");
        // Process the request
        // Prepare the response string
        return jQresponse;
    }

这是我的POJO Person.java

public class Person {
String name;
int age;

public Person(){

}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
    }

获取

    HTTP Status 415 -message-description-The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

我尝试了很多解决方案,但我无法找到错误。请在我出错的地方帮助我。

0 个答案:

没有答案