繁体   English   中英

XMLHTTPRequest响应不包含标头中的Location字段

[英]XMLHTTPRequest response doesnt contain Location field in the header

我试图修改adblockplus代码以进行测试。 我正在修改代码以在URL上发送http get请求并从响应中获取最终的URL。 我尝试使用以下代码,但响应不包含标头响应中的位置字段。 我在firefox扩展中这样做,所以我不认为跨域请求会有任何问题。 为什么我无法从响应中获取位置字段? 有没有更好的方法来完成这项任务?

输入网址 - http://netspiderads3.indiatimes.com/ads.dll/clickthrough?msid=17796167&cid=3224&slotid=1203&nsRndNo=817685688

预期产量 - http://www.mensxp.com/

实际输出 - 位置:null

这是我正在使用的代码 -

function geturl(url){
let req= Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
req.open("GET", url);
req.overrideMimeType("text/plain");
req.send(null);
req.onreadystatechange = function() {
if (req.readyState == 4) 
{ if (req.status == 200) 
  { try 
    {
       location = req.getResponseHeader("Location");
       console.log("Location is: " + location);
    }
    catch(e){
    console.log("Error reading the response: " + e.toString());
  }
 }
}

方案 -

我终于找到了解决方案。 我没有得到最后的答复。 所以我将重定向限制设置为0,现在我可以在标题中获取Location字段。 这是我添加到我的代码中的内容 -

if (request.channel instanceof Ci.nsIHttpChannel)
    request.channel.redirectionLimit = 0;

因为状态代码200表示正常,所以您的try块将不会被执行。 “位置”字段仅在重定向中退出,状态代码为301或302。

该url响应302,所以将req.status == 200更改为req.status == 302

实际上你必须检查readyState == 2 ,即HEADERS_RECEIVED状态

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM