innerhtml不在博客上工作

时间:2012-04-08 17:24:49

标签: javascript ajax innerhtml blogger

我正在研究一个简单的投票系统。当两个文件在一起(本地)时,它可以正常工作。

但是,当我在博客上发布它时,它无法输出结果。 (点击投票在webhost上注册,但结果不显示!)

继承我的代码:

<script type="text/javascript">
function getVote(int)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("poll").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","http://pacontest.eu.pn/poll_vote.php?vote="+int,true);
xmlhttp.send();
}
</script>
<div id="poll">
<h3>Do you like this?</h3>
<form>
Yes:
<input type="radio" name="vote" value="0" onclick="getVote(this.value)" />
No:
<input type="radio" name="vote" value="1" onclick="getVote(this.value)" />
</form>
</div>

1 个答案:

答案 0 :(得分:1)

innerHTML不起作用。这是你对外部网站的呼叫。您无法使用XMLHttpRequest从域外获取资源:它称为跨域限制,并且内置于浏览器规范中。

当PHP与GET的代码托管在同一个域上时,它可以正常工作,因为它不是跨域的。

您可以在域上使用代理脚本来解决限制:页面从该服务器端脚本请求结果,该脚本从真实位置获取结果,返回结果到浏览器。

这不太可能是Blogger的选项,因此Blogger提供了their own poll widget

相关问题