读书人

Struts2+ajax+json有关问题求解?

发布时间: 2012-08-17 02:08:34 作者: rapoo

Struts2+ajax+json问题,求解???
返回时一直为undefined,非常郁闷,我把代码贴出来,大家看看!
js代码:

JScript code
var url="${pageContext.request.contextPath}/updateHSwitch.action";    $.post(url,{isID:hsID,isSwitch:hsSwitch,dataType:"json",success:function(jsonres){     alert(jsonres); ? ? ? ? ? ? ?//?s??弹出??????undefined?     var json = eval("(" + jsonres + ")");         alert(json);? ? ? ? ?//?s??弹出??????undefined?     }      });


action代码:

Java code
 public String updateHSwitch() {  System.out.println(".............");  if(isID==100) return "ERROR";  dataList=hostSwitchBiz.getHostSwitch(isID);   //根据ID获取信息   for (int i = 0; i < dataList.size(); i++) {    hostswitch=(SwHostswitch)dataList.get(i);   }   hostswitch.setIsSwitch(isSwitch);    //替换开关状态数据   explanList=hostSwitchBiz.updateHSwitch(hostswitch);  //更新整条数据       JSONArray jsonres=JSONArray.fromObject(explanList);   System.out.println(jsonres.toString());?//??输出[{"isCommand":"#58008# ","isOpenExplain":"语音提示                     ","isState":"内部空号音提示","isSwitch":"0","isShutExplain":"听空好音                     ","isExplain":"内部空好音提示:1 语音提示,0 听空号音","isId":8}]   return  "jsonres";   }



xml配置:
XML code
<package name="json" namespace="/" extends="json-default">   <action name="updateHSwitch" class="hostSwitchAction" method="updateHSwitch">   <result name="jsonres" type="json" />  </action> </package>?



[解决办法]
直接访问${pageContext.request.contextPath}/updateHSwitch.action有输出没?
[解决办法]
$.post(url,{isID:hsID,isSwitch:hsSwitch,dataType:"json"},success:function(jsonres){


你上面这里是不是少了个},回调函数和发送的内容不在同一个object里面的
[解决办法]
你 用 firbug 看看 服务器端 给你返回的是什么数据?

读书人网 >Ajax

热点推荐