获取浏览器创建的请求的响应标头

时间:2017-12-21 19:52:35

标签: javascript xmlhttprequest eval

假设index.html的脚本具有外部js文件的URL( example.js ):

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net461" />
  <package id="AspNet.ScriptManager.bootstrap" version="3.0.0" targetFramework="net461" />
  <package id="AspNet.ScriptManager.jQuery" version="1.10.2" targetFramework="net461" />
  <package id="bootstrap" version="3.0.0" targetFramework="net461" />
  <package id="jQuery" version="1.10.2" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.0.6" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.Web" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.WindowsServer" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.AspNet.FriendlyUrls" version="1.0.2" targetFramework="net461" />
  <package id="Microsoft.AspNet.FriendlyUrls.Core" version="1.0.2" targetFramework="net461" />
  <package id="Microsoft.AspNet.ScriptManager.MSAjax" version="5.0.0" targetFramework="net461" />
  <package id="Microsoft.AspNet.ScriptManager.WebForms" version="5.0.0" targetFramework="net461" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.Web.Optimization.WebForms" version="1.1.3" targetFramework="net461" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.7" targetFramework="net461" />
  <package id="Microsoft.Net.Compilers" version="2.1.0" targetFramework="net461" developmentDependency="true" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
  <package id="Modernizr" version="2.6.2" targetFramework="net461" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net461" />
  <package id="Respond" version="1.2.0" targetFramework="net461" />
  <package id="WebGrease" version="1.5.2" targetFramework="net461" />
</packages>

我尝试过的是创建XMLHttpRequest,而不是用<html> <head> <script src="/example.js"></script> </head> <body></body> </html> 手动执行脚本。还有其他方法吗?

1 个答案:

答案 0 :(得分:0)

要向服务器发出请求时获取响应标头:

Vanilla JS

var client = new XMLHttpRequest();
client.open("GET", "/some_url", true);
client.send();
client.onreadystatechange = function() {
    if (this.readyState == this.HEADERS_RECEIVED) {
        console.log(client.getResponseHeader("some_header"));
    }
}

<强>的jQuery

$.ajax({
    type: 'GET',
    url: '/some_url',
    success: function(data, textStatus, request) {
        console.log(request.getResponseHeader('some_header'));
    },
    error: function(request, textStatus, errorThrown) {
        console.log(request.getResponseHeader('some_header'));
    }
});