Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.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文件未在HTML代码中启动_Javascript_Html_Ajax_Xmlhttprequest - Fatal编程技术网

Javascript js文件未在HTML代码中启动

Javascript js文件未在HTML代码中启动,javascript,html,ajax,xmlhttprequest,Javascript,Html,Ajax,Xmlhttprequest,我对JS比较陌生,目前正在研究XMLHttpRequests和闭包。我的主要目标是(1)让一个.js文件中的所有xhr代码(2)通过一个src标记将其拉入我的index.html页面,最后(3)调用index.html页面上的xhr函数 麻烦来了,当我尝试做第三。。。我可以调用我的函数在其本机.js文件上触发xhr,但不能在index.html中触发 以下是我到目前为止所写的内容: var experiment = function(callback){ return function

我对JS比较陌生,目前正在研究XMLHttpRequests和闭包。我的主要目标是(1)让一个.js文件中的所有xhr代码(2)通过一个src标记将其拉入我的index.html页面,最后(3)调用index.html页面上的xhr函数

麻烦来了,当我尝试做第三。。。我可以调用我的函数在其本机.js文件上触发xhr,但不能在index.html中触发

以下是我到目前为止所写的内容:

var experiment = function(callback){

    return function(destination){

        var xhr = new XMLHttpRequest();

        xhr.onload = function(){
            if(xhr.readyState === 4){
                if(xhr.status === 200){
                    var text = xhr.responseText;
                    callback(text);
                }

                else{
                    console.log(xhr.statusText);
                }

            }
        }


        xhr.open("get", destination, true);
        xhr.send(null);
    }
}

function closure(url){
    return experiment(alert)(url);
}
然后,在我的index.html页面中,输入如下内容:

<script type="text/javascript" src="exp.js">

    closure("url.com/json");

</script>

闭包(“url.com/json”);
但XHR从未开火。我做错了什么?


<script type="text/javascript" src="exp.js"/>
<script>

    closure("url.com/json");

</script>
闭包(“url.com/json”);
在调用closure()之前,您是否将js文件包含在html文件中?哇!好球!成功了。非常感谢你!!真不敢相信我把整件事都打印出来了,却没看到。