Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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文件检索响应头_Javascript_Ajax_Http_Http Headers - Fatal编程技术网

从外部javascript文件检索响应头

从外部javascript文件检索响应头,javascript,ajax,http,http-headers,Javascript,Ajax,Http,Http Headers,我正在寻找一种包含外部.js文件并从该请求接收响应头的方法 <script src="external/file.js?onload=callback"> function callback(data) { data.getAllResponseHeaders(); } </script> 这是我现在使用的代码 <script> function loadScript(url) { var oXmlHttp = new XMLHtt

我正在寻找一种包含外部.js文件并从该请求接收响应头的方法

<script src="external/file.js?onload=callback">
function callback(data) {
    data.getAllResponseHeaders();
}
</script>
这是我现在使用的代码

<script>
    function loadScript(url) {

    var oXmlHttp = new XMLHttpRequest();            
        oXmlHttp.withCredentials = true;
        oXmlHttp.responseType = 'text';
        oXmlHttp.open('GET', url, true);
        oXmlHttp.onload = function () {

          if( oXmlHttp.status >= 200 || oXmlHttp.status == XMLHttpRequest.DONE ) {

            var x = oXmlHttp.getAllResponseHeaders();
            console.log(x);

            if(oXmlHttp.responseText !== null) {

                var oHead = document.getElementsByTagName('HEAD').item(0);
                var oScript = document.createElement("script");
                    oScript.language = "javascript";
                    oScript.type = "text/javascript";
                    oScript.defer = true;
                    oScript.text = oXmlHttp.responseText;
                    oHead.appendChild(oScript);

            }

          }

        }
        oXmlHttp.send();
    }

    loadScript("http://url/to/file.js");        
</script>

函数加载脚本(url){
var oXmlHttp=new XMLHttpRequest();
oXmlHttp.withCredentials=true;
oXmlHttp.responseType='text';
open('GET',url,true);
oXmlHttp.onload=函数(){
if(oXmlHttp.status>=200 | | oXmlHttp.status==XMLHttpRequest.DONE){
var x=oXmlHttp.getAllResponseHeaders();
控制台日志(x);
if(oXmlHttp.responseText!==null){
var oHead=document.getElementsByTagName('HEAD')。项(0);
var oScript=document.createElement(“脚本”);
oScript.language=“javascript”;
oScript.type=“text/javascript”;
oScript.defer=true;
oScript.text=oXmlHttp.responseText;
oHead.appendChild(oScript);
}
}
}
oXmlHttp.send();
}
加载脚本(“http://url/to/file.js");        
:XMLHttpRequest.getAllResponseHeaders()方法将以字符串形式返回由CRLF分隔的所有响应头,如果未收到响应,则返回null。如果发生网络错误,则返回空字符串

这意味着您需要使用XMLHttpRequest加载外部js:

此外,通过这种方式,您只需加载文件一次

函数加载脚本(url){
var oXmlHttp=new XMLHttpRequest();
oXmlHttp.onreadystatechange=函数(){
if(oXmlHttp.readyState==XMLHttpRequest.DONE){
if(oXmlHttp.status==200){
var x=oXmlHttp.getAllResponseHeaders();
控制台日志(x);
if(oXmlHttp.responseText!=null){
var oHead=document.getElementsByTagName('HEAD')。项(0);
var oScript=document.createElement(“脚本”);
oScript.language=“javascript”;
oScript.type=“text/javascript”;
oScript.text=oXmlHttp.responseText;
oHead.appendChild(oScript);
}
}否则{
console.log(“错误”,oXmlHttp.statusText)
}
}
}
open('get',url);
oXmlHttp.send();
}
loadScript(“11.js?onload=callback”);

完全符合我的要求,谢谢!对于CORS,我添加了oXmlHttp.withCredentials=true;该函数在中创建3个脚本块。1为空,其他2个是它检索的javascript。我不明白它为什么会这样做。我稍微更改了您的脚本,最明显的是OnReadyState更改为onload,因为它会多次启动函数。@HugoCox您是对的。我的错误是混合了以下两个属性:status和readyState。非常感谢。代码段已更新
<script>
    function loadScript(url) {

    var oXmlHttp = new XMLHttpRequest();            
        oXmlHttp.withCredentials = true;
        oXmlHttp.responseType = 'text';
        oXmlHttp.open('GET', url, true);
        oXmlHttp.onload = function () {

          if( oXmlHttp.status >= 200 || oXmlHttp.status == XMLHttpRequest.DONE ) {

            var x = oXmlHttp.getAllResponseHeaders();
            console.log(x);

            if(oXmlHttp.responseText !== null) {

                var oHead = document.getElementsByTagName('HEAD').item(0);
                var oScript = document.createElement("script");
                    oScript.language = "javascript";
                    oScript.type = "text/javascript";
                    oScript.defer = true;
                    oScript.text = oXmlHttp.responseText;
                    oHead.appendChild(oScript);

            }

          }

        }
        oXmlHttp.send();
    }

    loadScript("http://url/to/file.js");        
</script>