0

Calling the following code in node 4.2.0, it's executed in a node cron job and not via terminal. The site I'm 'requesting' is 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 is just a simple phantom wrapper that recursively attempts to create a phantom object until successful. I'm confident svghost is not the issue. This works locally 100% of the time but I'm seeing this error when the cron job runs on our server: phantom stdout: NETWORK_ERR: XMLHttpRequest Exception 101: A network error occurred in synchronous requests.

Here's the stack trace:

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
Mark
  • 51
  • 1
  • 9
  • Have a look at [this](https://github.com/amir20/phantomjs-node/issues/345), it seems related and / or helpul. – gxx Jan 29 '16 at 11:40

1 Answers1

0

When you create your phantom instance using phantom.create you should add a --web-security=no argument or use the options object an add 'web-security':'no'to it.

It would result in something like this:

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

And no your NETWORK_ERROR should disappear

See this stack or tihs github issue for more informations.

Community
  • 1
  • 1
rels
  • 715
  • 2
  • 7
  • 22