ajax - When using @response and @request for json, I meet with some problems -


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/> 

i have imported jackson-annotations-2.1.4.jar, jackson-core-2.1.4.jar, jackson-databind-2.1.4.jar in lib cannot see print in console , shows "400 bad request" in chrome. have tried ways no effect.

you need convert javascript variable json before sending request server. json.stringify() conversion. based on code given above, should solve issue. hopefully.

$("#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("保存失败 ")         }     }) }); 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -