这是我的action代码
private String sreachAgent;//入库分销商
private String result;// 用户Ajax返回数据
public String AoutComp(){
mapCond.put("searchName", sreachAgent);
List<Agent> agentList=agentService.getAgentList(mapCond);
result=JsonUtil.arrayToJson(agentList);
return "jsonResult";
}
还有我的配置文件<package name="ajax" extends="json-default" namespace="/">
<action name="ajax-AoutComp" class="cardSaleAction" method="AoutComp">
<result name="jsonResult" type="json">result</result>
</action>
</package>关于json的导入包有 json-lib-2.3-jdk15.jar jsonplugin-0.34.jar
action里面已经得到agentList对象了,郁闷的是前台jquery收不到这个list对象,也没有报错,大家帮我看看我的配置有什么问题吗?
private String sreachAgent;//入库分销商
private String result;// 用户Ajax返回数据
public String AoutComp(){
mapCond.put("searchName", sreachAgent);
List<Agent> agentList=agentService.getAgentList(mapCond);
result=JsonUtil.arrayToJson(agentList);
return "jsonResult";
}
还有我的配置文件<package name="ajax" extends="json-default" namespace="/">
<action name="ajax-AoutComp" class="cardSaleAction" method="AoutComp">
<result name="jsonResult" type="json">result</result>
</action>
</package>关于json的导入包有 json-lib-2.3-jdk15.jar jsonplugin-0.34.jar
action里面已经得到agentList对象了,郁闷的是前台jquery收不到这个list对象,也没有报错,大家帮我看看我的配置有什么问题吗?
<action name="ajax-AoutComp" class="cardSaleAction" method="AoutComp">
<result name="jsonResult" type="json">result</result>
</action>
</package>
你这是何苦呢. 你返回字符串嘛,你搞个什么type=json? 我不知道你是否用的2.1.7+,
但是作为你把json以String的形式返回给客户端的话,
那么你在客户端需要用eval把json格式的串转成json
我最后有一句result=JsonUtil.arrayToJson(agentList); 已经转格式了呀
先前我用JsonUtil.objToJson(agentList)也试过JsonUtil,是我自己写的类,里面就是JSONArray.fromObject(array)和JSONObject.fromObject(object)
我用的struts2.1.6,第一次用struts2返回json, 都是在网上找的配置
那你就这样配置, 把你要返回的result设置成一个InputStream类型
action中
result=new StringBufferInputStream(json);
<package name="ajax" extends="struts-default" namespace="/">
<action name="ajax-AoutComp" class="cardSaleAction" method="AoutComp">
<result name="jsonResult" type="stream">
<param name="contentType">text/plain</param>
<param name="inputName">result</param>
</result>
</action>
</package>
呵呵有点晕,反正我就知道我这这么配是错的, 现在anction里面已经有了一个list,你就告诉我怎么能让前台的jquery接收到这个list吧,谢了 !
private String result;// 用户Ajax返回数据
改成private InputStream result; 添加get方法!
然后
配置文件这样
<action name="ajax-AoutComp" class="cardSaleAction" method="AoutComp">
<result name="jsonResult" type="stream">
<param name="contentType">text/plain</param>
<param name="inputName">result</param>
</result>
</action>
改 result=new StringBufferInputStream(JsonUtil.arrayToJson(agentList));
<param name="root">result</param>
<param name="includeProperties">str</param>
</result>Result result; 换成一个类的对象,然后类里面有个String str result = new Result;
result.setStr(agentList)result.add(str);我是用的这种方法
至于你上面的那种方法,既然已经转成json格式的了,就不用<result name="jsonResult" type="json">result</result>
我也看不懂,没用过
我按你的方法配置了,这是我的jquery代码function AoutCompSearchShance(obj) {
var id=$(obj).attr("id");
$.ajax({
type:"post",
url:"ajax-AoutComp.whbs",
data:{sreachAgent:$(obj).val()},
dataType:"text/plain",
success: function(date) {
alert("获取返回值"+data);
});
}
还是没有弹出来,好像就没有进function(date)这个函数,是不是dataType配置有问题?原来我配的“json”,刚改成了“text/plain”,都一样没反应
alert("获取返回值"+data);
});
你看看吧
type:"post",
url:"ajax-AoutComp.whbs",
data:{sreachAgent:$(obj).val()},
dataType:"text/plain",
success: function(date) {
alert("获取返回值"+data);
}
});
代码少个大括号喔.! 这样才是匹配的