Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax 在使用json的@response和@request时,我遇到了一些问题_Ajax_Json_Spring_Model View Controller - Fatal编程技术网

Ajax 在使用json的@response和@request时,我遇到了一些问题

Ajax 在使用json的@response和@request时,我遇到了一些问题,ajax,json,spring,model-view-controller,Ajax,Json,Spring,Model View Controller,LoginController.java: @Controller @RequestMapping("/user") public class LoginController { @RequestMapping(value="receive", method=RequestMethod.POST, consumes="application/json") @ResponseBody public RegInfo receiveData(@RequestBody RegInfo

LoginController.java:

 @Controller
@RequestMapping("/user")
public class LoginController {
@RequestMapping(value="receive", method=RequestMethod.POST, consumes="application/json")
    @ResponseBody
    public  RegInfo receiveData(@RequestBody RegInfo info){//

        System.out.println("come here");
        System.out.println(info.getRealname());
        return info;
    }
}
register.xml:

    $("#sub").click(function(){
    var m = {
            "realname": $("#real_name").val(),
            "phonenumber": $("#phone_number").val()             
        };
    $.ajax({
        type:"POST",
        url:"/demo/user/receive",
        data:m,

        dataType:"json",
        contentType:"application/json; charset=utf-8",
        async:false,
        success:function(data){
            alert("nihao");
        },
        erroe:function(data){
            alert("保存失败 ")
        }
    })
});
RegInfo.java:

public class RegInfo {

    private String realname;

    private String phonenumber;

    //private boolean sex;

    public RegInfo(){

    }

    public void setRealname(String realname){
        this.realname= realname;
    }
    public String getRealname(){
        return realname;
    }

    public void setPhonenumber(String phonenumber){
        this.phonenumber = phonenumber;
    }
    public String getPhonenumber(){
        return phonenumber;
    }
demo-servlet.xml:

<context:component-scan base-package="com.lhao.core"/>

<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven/>
<context:annotation-config/>


我已经在lib中导入了jackson-annotations-2.1.4.jar、jackson-core-2.1.4.jar、jackson-databind-2.1.4.jar,但是我在控制台中看不到打印,它在Chrome中显示“400错误请求”。我尝试了一些方法,但没有效果。

在将请求发送到服务器之前,需要将javascript变量转换为json。stringify()执行转换。基于上面给出的代码,这应该可以解决这个问题。希望如此

$("#sub").click(function(){
    var m = {
            "realname": $("#real_name").val(),
            "phonenumber": $("#phone_number").val()             
        };
    $.ajax({
        type:"POST",
        url:"/demo/user/receive",
        data:JSON.stringify(m),

        dataType:"json",
        contentType:"application/json; charset=utf-8",
        async:false,
        success:function(data){
            alert("nihao");
        },
        erroe:function(data){
            alert("保存失败 ")
        }
    })
});

400错误请求
通常在Spring/jackson无法将您的请求反序列化为给定类型时出现。我觉得你的模型还可以,所以我先尝试删除你的
@RequestMapping
中的
consumes
参数。谢谢。我已经解决了这个问题。首先我尝试了JSON.stringify(m),但没有效果。然后我替换了jackson-annotations-2.1.4.jar。。使用jackson-core-asl.jar和jackson-mapper-asl-1.8.5.jar,它确实有效。