2 shab1988 shab1988 于 2013.12.12 15:20 提问

ajax跨域 如何从动态生成的script中获得script的内容

** 已知服务器返回的是一个json格式的字符串,根据ajax跨域获得服务器json字符串如下: **
// 创建script标签,设置其属性
var script = document.createElement('script');

   script.setAttribute("type","text/javascript");

   script.setAttribute('src', temp);
script.setAttribute('id', "zz");

// 把script标签加入head,此时调用开始
document.getElementsByTagName('head')[0].appendChild(script); 

** html中已显示json字符串如下: **

{"Return":"{\"AwardsInfo\":[{\"LottType\":\"07\",\"IssueName\":\"2013121237\",\"IssueNo\":\"0\",\"RaffleNumber\":\"0215040701140905\",\"PrizeNo\":\"1\",\"BetAmount\":0,\"PrizePool\":0,\"PrizeInfo\":[{\"PrizeName\":\"选一数投\",\"BonusPerBet\":2500,\"BetCount\":0},{\"PrizeName\":\"选一红投\",\"BonusPerBet\":500,\"BetCount\":0},{\"PrizeName\":\"选二任选\",\"BonusPerBet\":800,\"BetCount\":0},{\"PrizeName\":\"选二连直\",\"BonusPerBet\":6200,\"BetCount\":0},{\"PrizeName\":\"选二连组\",\"BonusPerBet\":3100,\"BetCount\":0},{\"PrizeName\":\"选三任选\",\"BonusPerBet\":2400,\"BetCount\":0},{\"PrizeName\":\"选三前直\",\"BonusPerBet\":800000,\"BetCount\":0},{\"PrizeName\":\"选三前组\",\"BonusPerBet\":130000,\"BetCount\":0},{\"PrizeName\":\"选四任选\",\"BonusPerBet\":8000,\"BetCount\":0},{\"PrizeName\":\"选五任选\",\"BonusPerBet\":32000,\"BetCount\":0}]}],\"RetCode\":\"000000\"}","CheckSumCode":"dc53aa1ace464abae7aeee7590f56f2b"}

** 但是无论如何都获取不到$("#zz")里面json的内容,根据$("#zz").html()获取的是空 **

1个回答

cheng43526899
cheng43526899   2013.12.16 12:14

document.getElementsByTagName('head')[0].appendChild(script); 替换成document.getElementsByTagName('head')[0].innerHTML=script

Csdn user default icon
上传中...
上传图片
插入图片