ajax struts2 前后台 交互
1. 前台
$.get("/test/all.action",{'name':'name','password':'password'},function(data){
alert(data.name);
});
$.post("/test/all.action",{'name':'name','password':'password'},function(data){
alert(data.name);
});
var html = $.ajax({
url: "/test/all.action",
data: {'name':'name','password':'password'},
async: false
}).responseText;
console.log(eval( "("+html+")").name);
或者JSON.parse(html).name
后台
配置
<result-types>
<result-type name="json" class="org.apache.struts2.json.JSONResult"/>
</result-types>
action
@Results({ @Result(name = "success", type="json",params={"ignoreHierarchy","false","includeProperties","data.*,name"}),
@Result(name = "error", location = "/error.jsp") })
@Namespace("/test")
public class TestAction {
@Action(value="all")
public String all() {
data = new HashMap();
data.put("a", 1);
data.put("b", 2);
name = "myname";
return "success";
}
}