简体   繁体   English

Firefox扩展:使用XMLHttpRequest的多个请求。 是否使用异步?

[英]Firefox Extension: multiple requests with XMLHttpRequest. Use Asynchronous or not?

I'm trying something very simple for my first Firefox Add-On, the important part is: 对于第一个Firefox插件,我正在尝试一些非常简单的方法,重要的部分是:

Step 1) Call an external API to retrieve some data. 步骤1)调用外部API来检索一些数据。
Step 2) Call that API again with the data retrieved the first time to get some more. 第2步)使用首次获取的数据再次调用该API,以获取更多信息。

Now, I first implemented it using XMLHttpRequest in synchronous mode, since I thought the need to wait for Step 2 forced me to do it that way. 现在,我首先使用XMLHttpRequest在同步模式下实现了它,因为我认为需要等待步骤2迫使我这样做。 Two calls to the function that dealt with the API call, used XMLHttpRequest and parsed the response. 对处理该API调用的函数的两次调用使用XMLHttpRequest并解析了响应。 Fine. 精细。

Then I came accross various docs in the Mozilla Development Network which encourage you to use XMLHttpRequest in asynchronous mode and so I tried. 然后我遇到了Mozilla开发网络中的各种文档,这些文档鼓励您在异步模式下使用XMLHttpRequest,因此我尝试了。

Basing my implementation on multiple XMLHttpRequests and others I came up with the code below. 将我的实现基于多个XMLHttpRequests和其他对象,我想到了以下代码。

My question is: Is this the proper way to do it? 我的问题是:这是正确的方法吗? Should I go back to using synchronous mode? 我应该回到使用同步模式吗? It works like this, but it just doesn't strike me as the correct AJAX pattern you would use... 它的工作原理是这样的,但是它并没有让我成为您将要使用的正确AJAX模式...

  // first call
  var username = foo;
  var password = bar;
  var startOffset = 0; // initial value
  var url = encodeURIComponent('https://theapiurl.com/query=' + startOffset);
  doRequest();

  function doRequest() {
    makeRequest(url, username, password);
  }

  function makeRequest(url, username, password) {
    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, true, username, password);
    http_request.send(null);
  }  

  function alertContents(http_request) {
    if (http_request.readyState == 4) {
      if (http_request.status == 200) {
        if (startOffset == 0) {
          startOffset = 45; // this value would be extracted from 'http_request'
          url = encodeURIComponent('https://theapiurl.com/query=' + startOffset);
          // second call, parameter startOffset has changed
          doRequest();
        } else {
        }
      } else {
        alert('There was a problem with the request.');
      }
      http_request.onreadystatechange = function fnNull(){};
    }
  }

You should always avoid doing synchronous network requests as it will block the GUI from functioning until you get a response. 您应该始终避免执行同步网络请求,因为这将阻止GUI起作用,直到获得响应为止。 Just because the network may be fast for you, you should not assume it will be fast for all of your users. 仅仅因为网络对您来说是快速的,您不应该假设它对所有用户都是快速的。

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

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