节点4.2.0 phantom stdout:NETWORK_ERR:XMLHttpRequest异常101:同步请求中

Node 4.2.0 phantom stdout: NETWORK_ERR: XMLHttpRequest Exception 101: A network error occurred in synchronous requests

本文关键字:异常 XMLHttpRequest 同步 请求 ERR NETWORK phantom 节点 stdout      更新时间:2023-09-26

在节点4.2.0中调用以下代码,它在节点cron作业中执行,而不是通过终端执行。我"请求"的网站是http://www.milb.com/index.jsp?sid=t402.

module.exports.dynamicRequest = function(url, callback) {
  var makeDynamicRequest = function(attempt) {
    if (attempt === 4) {
      svghost.delPhantom();
      return callback(new Error('Phantom had 3 failures'));
    }
    svghost.getPhantom(function(err, ph) {
      if (err) {
        console.log(err.stack);
        setTimeout(function() {
          ph.exit();
          svghost.delPhantom();
          attempt++;
          makeDynamicRequest(attempt);
        }, Math.pow(2, attempt) * 300);
      } else {
        ph.createPage(function(page) {
          page.open(url, function(status) {
            if (status === 'success') {
              page.get('content', function(content) {
                ph.exit();
                svghost.delPhantom();
                callback(null, content);
              });
            } else {
              ph.exit();
              svghost.delPhantom();
              setTimeout(function() {
                attempt++;
                makeDynamicRequest(attempt);
              }, Math.pow(2, attempt) * 300);
            }
          });
        });
      }
    });
  };
  makeDynamicRequest(1);
};

svghost只是一个简单的phantom包装器,它递归地尝试创建一个phantom对象,直到成功为止。我相信svghost不是问题所在。这100%在本地工作,但当cron作业在我们的服务器上运行时,我看到了这个错误:phantom stdout: NETWORK_ERR: XMLHttpRequest Exception 101: A network error occurred in synchronous requests.

这是堆栈跟踪:

phantom stdout: /srv/apps/scraper/node_modules/spoton/node_modules/phantom/shim.js:7608 in send
/srv/apps/scraper/node_modules/spoton/node_modules/phantom/shim.js:7608 in _start
/srv/apps/scraper/node_modules/spoton/node_modules/phantom/shim.js:7635

使用phantom.create创建幻影实例时,应添加--web-security=no参数或使用options对象并向其添加'web-security':'no'

它会产生这样的结果:

phantom.create({'web-security':'no'}, callback...)

并且你的NETWORK_ERROR不应该消失

有关更多信息,请参阅此堆栈或tihs github问题。