【问题标题】:AJAX request only works once in IEAJAX 请求仅在 IE 中有效一次
【发布时间】:2011-04-07 01:03:00
【问题描述】:

我有这个问题,在 FF 中效果很好,但在 IE 中只能运行一次...... html是

<form>
<input type="button" value="test" onclick="javascript:vote();"/>
</form>

javascript

<script type="text/javascript">

function vote(){


if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();


  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

  }


xmlhttp.open("GET","../php/votes.php",true);
xmlhttp.send(null);

}

</script>

PHP 代码只是一个更新

<?php
$con = mysql_connect("localhost","mylog","mypass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db('versus',$con);

mysql_query("update picture_vs set votes = votes + 1");


?>

有什么想法吗?

【问题讨论】:

  • 第二次会发生什么?
  • 嗨!欢迎!您可以通过在每行的开头放置 4 个空格来格式化代码 (编辑器顶部的 {} 按钮就是这样做的);有关更多信息,请参阅stackoverflow.com/editing-help;;这次我已经为你完成了 :-) ;;我尝试重新添加您已删除的&lt;;如果我搞砸了,请随时重新编辑您的问题!
  • 缓存。该请求第二次不起作用,因为浏览器避免两次加载相同的内容。常见的解决方法是在 url 中附加一个随机 id。
  • @mario 所说的...或使用jQuery's ajax functions,保证可以跨浏览器工作并具有“无缓存”选项。
  • @65F 也许,但 jQuery 有一个明确的参数来关闭缓存。我认为它在内部所做的也只是在某些浏览器中添加一个随机 ID,但它是默默地这样做的。无论如何,为您的回答 +1

标签: php ajax internet-explorer xmlhttprequest


【解决方案1】:

将下面这行修改为...

xmlhttp.open("GET", "../php/votes.php?random=" + Math.random(), true);

这将阻止 IE 通过 URI 缓存您的请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2015-01-14
    • 2011-12-09
    相关资源
    最近更新 更多