为什么这个AJAX代码在IE8中不起作用?

时间:2011-11-29 21:22:42

标签: php javascript ajax internet-explorer-8 cross-browser

有人帮我解决了这个问题。我的ajax代码似乎与IE8浏览器不兼容。它没有任何其他浏览器的问题。功能是在每次点击“显示我的钱按钮”后,为“Snowboards Sold”和“Cash for the Slopes:”带来动态值。

来自随机值的代码是使用php生成的enter code here。我相信在代码中我无需改变,因为这是一个浏览器问题。

ajax代码如下:

 <head>
  <title>Boards 'R' Us</title>
  <link rel="stylesheet" type="text/css" href="boards.css" />
  <script type="text/javascript" src="text-utils.js"> </script>
  <script language="javascript" type="text/javascript">
   var request = null;
//Declaring object for browsers
   function createRequest() {
     try {
       request = new XMLHttpRequest();
     } catch (trymicrosoft) {
       try {
         request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (othermicrosoft) {
         try {
           request = new ActiveXObject("Microsoft.XMLHttp");
         } catch (failed) {
           request = null;
         }
       }
     }

     if (request == null)
       alert("Error creating request object!");
   }

//function called on clicking show me the money button clicked
   function getBoardsSold() {
     createRequest();
     var url = "getUpdatedBoardSales-ajax.php";
     request.open("GET", url, true);
     request.onreadystatechange = updatePage;
     request.send(null);
  }

  function updatePage() {
    if (request.readyState == 4) {
      var newTotal = request.responseText;
      var boardsSoldEl = document.getElementById("boards-sold");
      var cashEl = document.getElementById("cash");
      replaceText(boardsSoldEl, newTotal);

      /* Figure out how much cash Katie has made */
      var priceEl = document.getElementById("price");
      var price = getText(priceEl);
      var costEl = document.getElementById("cost");
      var cost = getText(costEl);
      var cashPerBoard = price - cost;
      var cash = cashPerBoard * newTotal;

      /* Update the cash for the slopes on the form */
      cash = Math.round(cash * 100) / 100;
      replaceText(cashEl, cash);
    }
  }
  </script>
 </head>

 <body>
  <h1>Boards 'R' Us :: Custom Boards Report</h1>
  <div id="boards">
   <table>
    <tr><th>Snowboards Sold</th>
     <td><span id="boards-sold">1012</span></td></tr>
    <tr><th>What I Sell 'em For</th>
     <td>$<span id="price">249.95</span></td></tr>
    <tr><th>What it Costs Me</th>
     <td>$<span id="cost">84.22</span></td></tr>
   </table>
   <h2>Cash for the Slopes: 
    $<span id="cash">167718.76</span></h2>
   <form method="GET">
    <input value="Show Me the Money" type="button"
           onClick="getBoardsSold();" />
   </form>
  </div>
 </body>
</html>

1 个答案:

答案 0 :(得分:1)

我可以推荐非常标准和广泛使用的JQUERY吗?

http://jquery.com/

http://docs.jquery.com/Downloading_jQuery

http://api.jquery.com/jQuery.get/

然后对于一个非常简单的魔术,替换关于创建AJAX和调用的部分:

$.get('getUpdatedBoardSales-ajax.php', updatePage);

保持你的updatePage功能,因为它已经设置好了!