SSH2项目中用jquery发送json,Action中接收不到。。
js代码:
$.ajax({
type:'post',
url:'/test.do',
data:{'name':'lin'},
datatype:'json',
success:function(result){
alert(result);
}
});
action中的java代码: StringBuffer json = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
json.append(line);
System.out.println("getJSON:" + line);
}
} catch (Exception e) {
System.out.println(e.toString());
}
tomcat控制台输出接收为空

解决方案 »

  1.   

    你能进入action,却接受不到数据,估计你后天接收有问题,没看到你action接收,前台传来的参数。
      

  2.   

     怎么我用jsp+servlet测试的是正确的,控制台输出了 getJSON:name=lin
    你的url没写错吧,有没有进入到后台呀
      

  3.   

    StringBuffer json = new StringBuffer();
    String line = null;
    try {
    BufferedReader reader = request.getReader();
    while ((line = reader.readLine()) != null) {
    json.append(line);
    System.out.println("getJSON:" + line);
    }
    } catch (Exception e) {
    System.out.println(e.toString());
    }
      

  4.   

    貌似得用request.getPara....获取吧。因为你前台都声明命名了!
      

  5.   

    确认你的url路径写的是否正确,能否到达action中,debug下。
      

  6.   

    js代码改了下:
    $.ajax({
    type:'post',
    url:'test.do',
    data:{id:'212',data:[{name:'chen',sex:'nan'},{name:'lin',sex:'nv'}]},
    datatype:'json',//改成html也一样
    });
    java代码加上:
    System.out.println("id:" + request.getParameter("id"));
    System.out.println("data:" + request.getParameter("data"));
    显示id:212    data:null
    如果上面data参数的值改为字符串:data:"[{name:....",后台就可以获取到data
    但这个应该是传字符串了,不是传json了吧