Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax脚本在IE8中失败_Javascript_Ajax_Internet Explorer 8 - Fatal编程技术网

Javascript Ajax脚本在IE8中失败

Javascript Ajax脚本在IE8中失败,javascript,ajax,internet-explorer-8,Javascript,Ajax,Internet Explorer 8,以下脚本在Safari、Chrome和Firefox中执行并工作良好,但在IE8中则不行。不幸的是IE8是我的目标浏览器之一,所以这有点问题。因为我对Ajax没有太多经验,所以我也不确定从哪里开始寻找 我注意到IE在第15行(标有**)上报告了一个错误,这并没有真正意义,因为if-else应该阻止它查看该行 function getNames(str) { var xmlhttp; // Clear previous queries if(str.length == 0)

以下脚本在Safari、Chrome和Firefox中执行并工作良好,但在IE8中则不行。不幸的是IE8是我的目标浏览器之一,所以这有点问题。因为我对Ajax没有太多经验,所以我也不确定从哪里开始寻找

我注意到IE在第15行(标有**)上报告了一个错误,这并没有真正意义,因为if-else应该阻止它查看该行

function getNames(str) {
    var xmlhttp;
    // Clear previous queries
    if(str.length == 0){
        document.getElementById("txtHint").innerHTML = "";
        return;
        // Due to the high number of possible hits we'll demand 3 chars
        // before we start querying.
    }else if(str.length < 3){
        return ;
    }
    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.status == 200 && xmlhttp.readyState == 4){
            // String from get_names.php is comma separated
            var arr = xmlhttp.responseText.split(",");
            // The UL list we want our names in
            var ul = document.getElementById("names");

            // Clear the list for each key in
            if(ul.hasChildNodes()){
                while(ul.childNodes.length >= 1){
                    ul.removeChild(ul.firstChild);
                }
            }
            // Step trough the String in Array form
            for(var i = 0; i < arr.length; i++){
                // :@ means that we've reached the end of applicable names.
                if (arr[i] != ":@") {
                    var li = document.createElement("li");
                    li.innerHTML = newListItem = arr[i];
                    // Inserts the current name into the list.
                    ul.insertBefore(li, ul.getElementsByTagName("li")[0]);
                }
            }
        }
    }
    xmlhttp.open("GET", "./ext/get_names.php?q=" + str, true);
    xmlhttp.send();
}
函数getNames(str){
var-xmlhttp;
//清除以前的查询
如果(str.length==0){
document.getElementById(“txtHint”).innerHTML=“”;
返回;
//由于可能的命中率很高,我们需要3个字符
//在我们开始查询之前。
}否则如果(str.length<3){
返回;
}
if(window.XMLHttpRequest){//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
**xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”)**
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.status==200&&xmlhttp.readyState==4){
//get_names.php中的字符串用逗号分隔
var arr=xmlhttp.responseText.split(“,”);
//我们想要的UL名单
var ul=document.getElementById(“名称”);
//清除中每个键的列表
if(ul.hasChildNodes()){
而(ul.childNodes.length>=1){
ul.removeChild(ul.firstChild);
}
}
//以数组形式跳转字符串
对于(变量i=0;i
您应该首先检查readyState,然后检查状态。这是一个常见的错误,在大多数浏览器中都会报告但被忽略。我不确定这是否是您问题的解决方案,但由于您没有提供错误消息,因此很难进一步帮助您

if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
   // Code ...
}

据我所知,“window.XMLHttpRequest”-检查应该可以


你们可以试着看看答案。在这种情况下,问题是在浏览器设置中禁用了原生xmlhttprequest。

Stock回答:您是否考虑过为此使用jQuery之类的框架?他们会为你处理所有的跨浏览器的SunNigigaS,而不是直接回答你的问题,但是也许你应该考虑一个JS框架(jQuery/MOOToSo/…)。这些跨平台工作,并为您省去了麻烦您是否可以添加逻辑来测试activex对象是否可用,即:(if(window.ActiveXObject))为什么顺序很重要?除非读取
readyState
会对
状态产生副作用,否则不应执行此操作。。。这很愚蠢:-/(请注意,这只是一个逻辑连接:
&&
),因为只有当readState等于4(或3)时才能访问属性“status”。当readyState不是这些值之一时,访问变量至少会在Chrome和Firefox中引发错误。对,那么为什么
xmlhttp.readyState==4&&xmlhttp.status==200
不同于
xmlhttp.status==200&&xmlhttp.readyState==4
(在文章中)?虽然我同意这是一个更符合逻辑的布局,假设没有副作用,
a&&b
b&&a
在逻辑上是等价的。这是正确的,但执行顺序才是相关的。首先计算xmlhttp.readyState==4,如果它为false,浏览器将从if语句返回,因为无论xmlhttp.status==200的结果是什么,if子句都将返回false。这不是关于返回的布尔值,而是关于访问属性的时间。请参阅我以前的评论:假设没有副作用,
a&&b
b&&a
在逻辑上是等效的。鉴于这一事实,在这种情况下,为什么订单很重要?