简体   繁体   English

Firefox扩展-每页多个XMLHttpRequest调用

[英]Firefox Extension - Multiple XMLHttpRequest calls per page

I am trying to create a Firefox extension that can run multiple XMLHttpRequests per page. 我正在尝试创建一个可以在每个页面上运行多个XMLHttpRequest的Firefox扩展。 The code is below (my main function calls the makeRequest on different URLs). 下面的代码(我的主要功能在不同的URL上调用makeRequest)。 My problem is that it always returns (at the "alert('Found ...')" for debugging purposes) the same URL instead of displaying the different responses. 我的问题是,它总是返回相同的URL(在“ alert('Found ...')”处用于调试),而不显示不同的响应。 I think the issue is that I should somehow pass the http_request instance to the alertContents() function instead of just using http_request directly, but not sure how or if this is correct. 我认为问题在于我应该以某种方式将http_request实例传递给alertContents()函数,而不是直接使用http_request,但是不确定如何或是否正确。 Thank you. 谢谢。

function makeRequest(url,parameters) {
   http_request = false;
   http_request = new XMLHttpRequest();
   if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }
   http_request.onreadystatechange = alertContents;
   http_request.open('GET', url + parameters, true);
   http_request.send(null);

}

function alertContents() {
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
            alert('Found: ' + http_request.responseText);
      } else {
         alert('There was a problem with the request.');
      }
   }
}

Your problem is you've only got one http_request identifier which is reused every time the makeRequest function is called. 您的问题是您只有一个http_request标识符,每次调用makeRequest函数时都会重复使用该标识符。 Here is one simple adjustment:- 这是一个简单的调整:

function makeRequest(url,parameters) {
   var http_request = new XMLHttpRequest();
   if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }
   http_request.onreadystatechange = function() { 
           alertContents(http_request)
       };
   http_request.open('GET', url + parameters, true);
   http_request.send(null);
       return http_request;
}

function alertContents(http_request) {
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
            alert('Found: ' + http_request.responseText);
      } else {
         alert('There was a problem with the request.');
      }
              http_request.onreadystatechange = fnNull;
   }
}

    function fnNull() { };

The http_request identifier is local to each makeRequest execution. http_request标识符对于每个makeRequest执行都是本地的。 The correct instance of XHR is then passed to alerrContents each time onreadystatechange is fired by using a capture. 每次使用捕获触发onreadystatechange时,XHR的正确实例就会传递到alerrContents。

BTW, why separate url from parameters? 顺便说一句,为什么将网址与参数分开? Since the caller has to ensure the parameters argument is correctly url encoded it doesn't seem like a very useful abstraction. 由于调用者必须确保parameters参数正确进行了url编码,因此它似乎不是非常有用的抽象。 In addition the caller can simply pass a URL containing a querystring anyway. 另外,调用者仍然可以简单地传递包含查询字符串的URL。

this function can be further improved with cross browser functionality: 可以通过跨浏览器功能进一步改进此功能:

function makeRequest(method, url, parameters) {
    var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            // set type accordingly to anticipated content type
            http_request.overrideMimeType('text/xml');
            //http_request.overrideMimeType('text/html');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
        try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
        }
    }
    if (!http_request) {
        alert('Cannot create XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = function() {
        alertContents(http_request);
    }
    url += (method=="GET")?parameters:"";
    http_request.open(method, url, true);
    if (method == "POST") {
        http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http_request.setRequestHeader("Content-length", parameters.length);
        http_request.setRequestHeader("Connection", "close");
    }
    http_request.send((method=="GET")?null:parameters);
}

Yes, you should use the same XMLHttpRequest . 是的,您应该使用相同的XMLHttpRequest

Infact, try using this code and see if it works : 实际上,请尝试使用以下代码,看看它是否有效:

function makeRequest(url,parameters) {
   http_request = false;
   http_request = new XMLHttpRequest();
   if (http_request.overrideMimeType) {
          http_request.overrideMimeType('text/xml');
   }
   if (!http_request) {
          alert('Cannot create XMLHTTP instance');
          return false;
   }
   http_request.onreadystatechange = function () {
       if (http_request.readyState == 4) {
          if (http_request.status == 200) {
                        alert('Found: ' + http_request.responseText);
          } else {
                 alert('There was a problem with the request.');
          }
       }
   };
   http_request.open('GET', url + parameters, true);
   http_request.send(null);
}

In the code above, I simply attached the function directly to the onreadystatechange event . 在上面的代码中,我只是将函数直接附加到onreadystatechange事件。

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

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