[英]How to Handle XMLHttpRequest Exception 101 Error in PhantomJS?
我是JavaScript和PhantomJS的新手。 当我运行myfile.js(熊for
与命令循环) phantomjs myfile.js
,它有时(但绝对会)给出了
NETWORK_ERR: XMLHttpRequest Exception 101: A network error occurred in synchronous requests.
每次发生此错误时,我都必须终止此过程(在终端中使用Ctrl + C),然后使用下面的bash命令来继续我的工作
$ some bash command to clean the job done before error occurs
$ phantomjs myfile.js
这真的很令人沮丧,尤其是我有一个大的for
循环要做。 所以,我想知道是否有某种方法可以在每次发出时自动执行这些行
NETWORK_ERR: XMLHttpRequest Exception 101
phantomjs myfile.js
粗略的考虑,我可能在myfile.js中有一些错误处理代码,或者将phantomjs myfile.js
嵌入到shell脚本文件中并捕获到它发生的错误。
有人可以教我该怎么做吗?
这是我的主要PhantomJS代码:
// myfile.js
var request = new XMLHttpRequest();
var myURLs = ["url1","url2", ... ]; // this array contains more than 10k URLs
for (i=0; i<myURLs.length; i++) {
request.open('GET', myURLs[i], false); // synchronous request
request.setRequestHeader("HEADERKEY","HEADERVALUE");
request.send();
if (request.status === 200) {
console.log(request.responseText);
} else {
console.log("Error Code: " + request.status);
phantom.exit();
}
}
phantom.exit();
出于私人原因,我无法共享myURLs
数组内的URL。 对此感到非常抱歉,但是如果您对我的代码有所了解,我仍然会寻求帮助。
igh,我得在这里回答我自己的问题。
PhantomJS提供了一个名为onError
的全局错误处理程序。 当phantomjs抛出错误时,它非常有用。
您可以在此处找到详细信息: http : //phantomjs.org/api/phantom/handler/on-error.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.