前台按钮单击事件:
[code="java"]
$("#cmd_test").click(function (){
alert("cmd_test");
$.ajax({
url: "testActionJson.action",
dataType:"json",
type:"post",
data: {"id":1,"name":"john","age":30},
success: function(data){
alert(data);
},
error:function(request,msg,errObj){
alert(msg);
}
});
[/code]
[code="java"]
后台Action中的testActionJson方法:
public void testActionJson() {
ActionContext ac=ActionContext.getContext();
HttpServletRequest request=(HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response=(HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);
System.out.println("testActionJson");
String name=request.getParameter("name");
System.out.println(name);
try {
response.getWriter().print(name);
} catch (Exception e) {
System.out.println(" error:");
e.printStackTrace();
}
}
[/code]
奇怪的是在单击按钮提示parsererror错误,但跟踪发现是有从后台action中返回name变量值john到后前台的,在后台输出也是有值的,但为什么发生error呢。