如何获取xmlhttp请求的URL(AJAX) [英] How to get the URL of a xmlhttp request (AJAX)

查看:149
本文介绍了如何获取xmlhttp请求的URL(AJAX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

w3schools.com (url)上有一个例子如何使用纯Javascript执行AJAX调用。如果你看看这个例子,你会看到这个调用是由一个按钮触发的:

 < button type =buttononclick =loadXMLDoc()>更改内容< / button> 

这是函数:

<$ p $ ()
{
var xmlhttp;
if(window.XMLHttpRequest)
{//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else
{//代码为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()希望它能为我提供主机,例如:

  if(xmlhttp.readyState == 4&& xmlhttp.status == 200)
{
document.getElementById(myDiv) .innerHTML = xmlhttp.responseText;
alert(xmlhttp.getAllResponseHeaders());

它确实给我所有的头文件,但不包含主机。所以我的下一步是尝试使用 setRequestHeader()来设置主机,但后来我意识到Header需要一个我必须发送自己的值,所以这不起作用。我还有什么可以尝试在alert中获取/取出传出的AJAX URL?

请注意,代码仅仅是一个例子,我知道由于Access-Control-Allow-Origin的原因,更改头文件(在这种情况下)是被禁止的。

解决方案

我不确定您有多少代码访问权限,但您可以覆盖

  XMLHttpRequest.prototype.open =(function(open())打开XMLHttpRequest.open  ){
return function(method,url,async){
console.log('outgoing url is',url);
open.apply(this,arguments);
};
})(XMLHttpRequest.prototype.open);

这是 FIDDLE


On w3schools.com(url) there is an example of how to do an AJAX call with plain Javascript. If you look at the example you will see the call is triggered by a button:

<button type="button" onclick="loadXMLDoc()">Change Content</button>

This is the function:

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();
}

What I would like to do is get the URL of the outgoing AJAX call which is ajax_info.txt(url):

xmlhttp.open("GET","ajax_info.txt",true);

Im trying to put that URL in to an alert, so I tried calling the headers of the response using getAllResponseHeaders() hoping that it will give me the Host like so:

if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    alert(xmlhttp.getAllResponseHeaders());

It does give me all the headers but not the Host. So my next move was trying to set the Host myself using setRequestHeader() but then I realized the Header needs a Value which I had to send myself, so this will not work. What else can I try to get/fetch the outgoing AJAX URL in the alert?

Please note the code is just an example and I know that changing headers(in this case) is prohibited because of Access-Control-Allow-Origin.

解决方案

I'm not sure how much access you have to the code but you can over-ride XMLHttpRequest.open and hook the url there.

XMLHttpRequest.prototype.open = (function(open) {
  return function(method,url,async) {
      console.log('the outgoing url is ',url);
      open.apply(this,arguments);
    };
})(XMLHttpRequest.prototype.open);

Here is a FIDDLE.

这篇关于如何获取xmlhttp请求的URL(AJAX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆