struts.xml里面配置
<package name="ssh" extends="json-default" namespace="/">

<action name="user_*" class="userAction" method="{1}">     <result name="addUser" type="json"/>
    
</action>
</package>action里面部分代码
                Map map = new HashMap();
map.put("flag", flag);
result = JSONObject.fromObject(map).toString();
System.out.println("json...... "+result);
return "addUser";
}
//name:客户端获取的json格式数据的名字
@JSON(name="result")
public String getResult() {
return result;
}
页面部分代码
$.getJSON(url,function(data){

    var d = eval( "("+data.result+")" );
     alert( d.flag );
    if( d.flag == 0 ){
        
     alert( "数据添加失败" );
    }else{
     alert("数据添加成功");
    }

});
就是页面点击提交按钮后,进了action可以得到result,也输出了,就是页面没得到json返回数据,没反应???
页面那个getJSON完全没有进来?请教是什么原因???

解决方案 »

  1.   

     Map map = new HashMap();
    map.put("flag", flag);
    result = JSONObject.fromObject(map).toString();
    System.out.println("json...... "+result);
    return "addUser";
    }
    后台代码中result 并没有response.wirte() 并未返回到前台,
    你可以通过火狐进入查看返回的信息
      

  2.   

    好像是 response.getWirte.print()吧
      

  3.   

    既然是用ajax,那么为什么没有像前台输出了?还有你用Ajax请求,页面不应该跳转的呀。为什么还要
    return 了?
      

  4.   

    页面是不要跳转,是那个页面要接受action处理完的数据,显示我是否操作成功,要给个反应,但没有?
      

  5.   


            HttpServletResponse response = ServletActionContext.getResponse();   
     
            response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");   
            PrintWriter out = response.getWriter();   
            System.out.println(resultString);
            //直接输出响应的内容   
            out.println(resultString);   
            out.flush();   
            out.close();    
            
            return null;
      

  6.   

    $(document).ready(function() {       
    $.ajax({ 
             url : "user_*",  
                 type : 'POST',  
                 dataType : 'text',         success: function(data, textStatus){ 
            
            }, 
            complete: function(XMLHttpRequest, textStatus){ 
            
            }, 
            error: function(error){ 
                ///请求出错处理              
                alert('错误信息:'+error.status);
            } 
    });     
      
    }); js 可以加"debugger" 进行调试,也有可能没有找到action
      

  7.   

    回复5楼:我是ssh整合,用的是struts2,response是封装好了的,不需要写,
    回复5楼:action进来了,里面打印了数据,就是页面的不到传过来的值?不知道是没传?还是传了接收不到?
      

  8.   

    其实你用$.ajax 试一试就知道问题出在哪里了,因为你用的那个是直接json不容易调试,换个方式调试下就好了
      

  9.   

    你把action输出个简单的json对象或者json字符串,应该是提交前台的json有错误,可能换行引号之类。
    实在不行自己拼接个简单的字符串(数字和字母,不含中文和html代码)提交给前台
      

  10.   

    action方法得到的json...... {"flag":1}没错啊???
      

  11.   

    数据库中数据添加进去了,action也输出了JSON数据,就是页面中的$.getJSON()方法没进去??求解?
      

  12.   

    我在struts.xml配置文件里面加入
     <result name="add" type="json"> 
                <param name="includeProperties">result</param></result>
    就可以了,action方法里面就不需要注解,这里不懂
      

  13.   

    struts.xml配置文件中写ajax请求要返回的json数据