IE8打破了我的AJAX ...... FF很好

时间:2009-10-26 17:48:51

标签: javascript ajax firefox internet-explorer-8

在使用AJAX提交创建表单后感到非常自豪,我在IE8中测试它并获得“消息:'数量'未定义”。我已经读到它可能与早期版本的IE使用ActiveX用于AJAX请求这一事实有关,但我是JS的新手并没有真正理解这个问题,更不用说实现修复的能力了

这是我的代码:

var time_variable;

function getXMLObject()  //XML OBJECT
{
  var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject();   //xmlhttp holds the ajax object

function ajaxFunction() {
  var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) { 
    var txtname = document.getElementById("txtname");
    xmlhttp.open("POST","slots.php",true); //calling     testing.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("quantity=" + quantity.value + "&price=" + price.value + "&slot=" +         slot.value + "&store=" + store.value); //Posting txtname to PHP File


  }
}

function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the     HTML Form element 
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}

2 个答案:

答案 0 :(得分:1)

如果数量是表单字段,则需要在使用之前使用getElementById获取它,就像使用txtname一样:


var quantity = document.getElementById("quantity");

您无法直接在表单中使用它。

答案 1 :(得分:1)

根据您对上述问题的最后评论,我怀疑您并未在任何地方定义“数量”,并假设它将引用表单字段。试试这个:

if(xmlhttp) { 
    var txtname = document.getElementById("txtname");
    var quantity = document.getElementById("quantity");
    var price = document.getElementById("price");
    var store = document.getElementById("store");
    xmlhttp.open("POST","slots.php",true); //calling     testing.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("quantity=" + quantity.value + "&price=" + price.value + "&slot=" +               slot.value + "&store=" + store.value); //Posting txtname to PHP File
}