【发布时间】:2014-01-12 01:30:14
【问题描述】:
在w3schools.com(url) 上有一个如何使用纯Javascript 进行AJAX 调用的示例。如果您查看示例,您会看到调用是由按钮触发的:
<button type="button" onclick="loadXMLDoc()">Change Content</button>
这是函数:
function loadXMLDoc()
{
var xmlhttp;
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.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
我想做的是获取传出 AJAX 调用的 URL,即ajax_info.txt(url):
xmlhttp.open("GET","ajax_info.txt",true);
我试图将该 URL 放入警报中,因此我尝试使用 getAllResponseHeaders() 调用响应的标头,希望它会给我这样的 Host:
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
alert(xmlhttp.getAllResponseHeaders());
它确实给了我所有的标题,但没有给我主机。所以我的下一步是尝试使用setRequestHeader() 自己设置主机,但后来我意识到标头需要一个我必须自己发送的值,所以这不起作用。我还能尝试在警报中获取/获取传出 AJAX URL 吗?
请注意代码只是一个示例,我知道由于 Access-Control-Allow-Origin 而禁止更改标题(在这种情况下)。
【问题讨论】:
-
我知道它们不是最好的,我只是以它们为例
标签: javascript jquery ajax get http-headers