JS eval函数(后台组装成对象,前台 EVAL 接收,转换成需要的对象属性值)必须加载附件
第一步:后台action 把需要的数据集转换成:
[{name:'value',pwd:'value'},{name2:'value',pwd2:'value'}] 格式的字符
public String lotteryList(){
int actityId=1;
List list=lotteryDrawService.getLotteryList(actityId);
String str = "";
if(list != null && list.size() > 0){
str += "[";
for(int i = 0 ; i < list.size(); i++){
Map m = (Map) list.get(i);
str += "{prizeDesc:'" + m.get("PRIZE_DESC") + "',";
if(i + 1< list.size()){
str += "winningUserPhone:'" + m.get("WINNING_USER_PHONE") + "'},";
}else{
str += "winningUserPhone:'" + m.get("WINNING_USER_PHONE") + "'}";
}
}
str += "]";
}
this.setWriterPrint(str);
return null ;
}
第二步:前台JS 代码,解析后台需要的集合属性值
$(document).ready(function(){
getLotteryList();
});
function getLotteryList(){
var uri = "http://bj.homescn.com/lotteryDraw/lotteryList.do?actityId=1";
send_request(uri, function(data){
if(typeof(data) !='undefined' && data != ''){
var arr = eval(data);
var html = "<marquee onMouseOver=\"this.stop()\" onMouseOut=\"this.start()\" align=center direction=up scrollamount=1 scrolldelay=3 valign=middle behavior=\"scroll\">";
for(var i = 0; i < arr.length; i++){
var obj = arr[i];
html += "<li>" + obj.prizeDesc + " " + obj.winningUserPhone +"</li>";
}
html += "</marquee>";
document.getElementById("marquee_list").innerHTML = html;
}
}, false);
}