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 JS文件在IE上运行良好,但在其他浏览器上不起作用_Javascript_Ajax - Fatal编程技术网

Javascript JS文件在IE上运行良好,但在其他浏览器上不起作用

Javascript JS文件在IE上运行良好,但在其他浏览器上不起作用,javascript,ajax,Javascript,Ajax,下面的函数在ie中获得结果,但在其他浏览器上没有。有什么建议吗 function show_packet(str, company) { var cam = document.getElementById("company"); if (window.XMLHttpRequest) { var xmlhttp = new XMLHttpRequest(); } else { var xmlhttp = new ActiveXObject("

下面的函数在ie中获得结果,但在其他浏览器上没有。有什么建议吗

function show_packet(str, company) {
    var cam = document.getElementById("company");
    if (window.XMLHttpRequest) {
        var xmlhttp = new XMLHttpRequest();
    } else {
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("packet_1").innerHTML = xmlhttp.responseText;
            document.getElementById("icon_1").innerHTML = "";
        }
    }
    xmlhttp.open("GET", "show_packet.php?car_moto=" + encodeURIComponent(str, true) + "&cam=" + encodeURIComponent(cam.value, true));
    xmlhttp.send();
}

VAR和函数被提升到其包含函数的顶部。在顶部声明一次您的var,并仅在if/else中分配。

您在控制台中看到了什么?在ie 10上,在控制台上是ok的。在Modzilla Web控制台上,它似乎运行了2次文件,我看不出这在这里是如何应用的。我认为其效果可能是它会通过提升赋值来覆盖该值。我只是尝试了一个测试,但事实并非如此。Javascript没有与Java/C++等相同的作用域规则