doqrt26664 2012-05-06 06:52
浏览 42
已采纳

Ajax无法与Internet Explorer一起正常运行

I'm currently working with an e-commerce shopping site where different products require to be rated. I'm using a star rating script.

Everything is working fine but a product should be rated only once according to the visitor's IP and once the visitor clicks a star (among the five stars), all the stars should be disabled so that duplicate rating for the same product with the same IP can be prevented (I'm also using serve side validations) and average rating according to the new value from the database should be indicated by the same stars (which have just been disabled).

It's working on Firefox with no problem at all. When a visitor clicks a star a new value is passed to the database (using Ajax) and according to the new value, average rating is calculated and displayed but Internet explorer is unable to retrieve the new value from the database using Ajax.

I'm just demonstrating the problem with very simple code as follows.

Following is Temp.php file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript" language="javascript">
    var xmlhttp;        
    function ajax()
    {
        if(window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            xmlhttp = new ActivexObject("Microsoft.XMLHTTP");
        }
    }

    function loadStars(prod_id)
    {
        ajax();

        xmlhttp.onreadystatechange=function()
        {           
            if(xmlhttp.readyState==4 && xmlhttp.status==200)
            {                               
                document.getElementById("ajax_response").innerHTML=xmlhttp.responseText;
                var rating= document.getElementById("rating_value").value;              
                alert(rating);   //Rating value of hidden field from the ajax response is alered.               
            }
        }

        var queryString="Temp1.php?prod_id="+prod_id;
        xmlhttp.open("GET", queryString, true);
        xmlhttp.send();             
    }
</script>
</head>

<body onload="loadStars(11);">
    <span id="ajax_response"></span>
</body>
</html>

and the following is the Temp1.php


<?php
    include_once("../Connection.php");  
    $con=new Connection();
    $con->get_connection(); 

    if(isset($_GET['prod_id']))
    {           
        $result=mysql_query("select rating_num from rating where prod_id=".$_GET['prod_id']."");
        $rating=mysql_result($result, 'rating_num');        
        echo "<input type='hidden' id='rating_value' name='rating_value' value='$rating'/>";                                                                    
    }
?>

The code in both the files has nothing to do with. The js function loadStars(prod_id) is called on the onload event (look at the body tag) which actually invokes the Ajax request to the Temp1.php which retrieves rating_num from the database and simply stores into a hidden field named rating_value which is finally alerted on the Temp.php file using alert(rating);

The actual question is here when the value of rating_num is changed in the database, Firefox displayed the updated value which is essential BUT Internet explorer (8) still displays the old value even though the page is refreshed and reload again and again.

What should be the reason? Is there any solution to this problem? Hope you will be able to understand what I mean.

  • 写回答

1条回答 默认 最新

  • dporu02280 2012-05-06 07:15
    关注

    Internet Explorer does some funny stuff with caching sometimes. It tends to take a much more intense approach to caching than other browsers. The cache is a great thing when we normally access the same resource twice, three times, or more. In the cases of static content, we actually want the browser to be smart enough to not request data it already has retrieved, as this saves time and bandwidth.

    However, AJAX requests are different in that they are generally dynamic. The same request made at one moment in time may of course yield a completely different result at another moment in time. As a result, we don't normally want to cache an AJAX request.

    However, IE's intense caching becomes a problem when it comes to AJAX requests. The browser treats an AJAX request just like it would a request for a static, unchanging image, and it pulls the previous results of that request from it's cache as if nothing has changed.

    This is of course not something that you want Internet Explorer to do. Thus, the quickest and simplest technique you can use to force IE to pull fresh data is to make a slight modification to the URL each time.

    Lucky for us, time is something that constantly moves forward, and by its very nature, it is guaranteed to be unique. Here is how you can avoid this problem:

    var queryString="Temp1.php?prod_id="+prod_id + "&t=" + new Date().getTime();
    

    By appending the epoch time to the end of the query string, we ensure that the URL will always be unique, ensuring that the browser always grabs fresh content from the server.

    I use this technique on all of my AJAX requests and have found it to be extremely valuable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题