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完全没有进来?请教是什么原因???
<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完全没有进来?请教是什么原因???
map.put("flag", flag);
result = JSONObject.fromObject(map).toString();
System.out.println("json...... "+result);
return "addUser";
}
后台代码中result 并没有response.wirte() 并未返回到前台,
你可以通过火狐进入查看返回的信息
return 了?
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;
$.ajax({
url : "user_*",
type : 'POST',
dataType : 'text', success: function(data, textStatus){
},
complete: function(XMLHttpRequest, textStatus){
},
error: function(error){
///请求出错处理
alert('错误信息:'+error.status);
}
});
}); js 可以加"debugger" 进行调试,也有可能没有找到action
回复5楼:action进来了,里面打印了数据,就是页面的不到传过来的值?不知道是没传?还是传了接收不到?
实在不行自己拼接个简单的字符串(数字和字母,不含中文和html代码)提交给前台
<result name="add" type="json">
<param name="includeProperties">result</param></result>
就可以了,action方法里面就不需要注解,这里不懂