Javascript 如何在PhantomJS中处理XMLHttpRequest异常101错误?
我是JavaScript和PhantomJS的新手。当我使用命令Javascript 如何在PhantomJS中处理XMLHttpRequest异常101错误?,javascript,shell,error-handling,xmlhttprequest,phantomjs,Javascript,Shell,Error Handling,Xmlhttprequest,Phantomjs,我是JavaScript和PhantomJS的新手。当我使用命令phantomjs myfile.js运行myfile.js(为循环加载)时,它偶尔(但肯定会)发出 NETWORK_ERR: XMLHttpRequest Exception 101: A network error occurred in synchronous requests. 每次发生此错误时,我都必须终止此过程(在终端中使用Ctrl+C),然后使用下面的bash命令继续我的工作 $ some bash command
phantomjs myfile.js
运行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
这真的很令人沮丧,尤其是我有一个很大的循环要做。所以,我想知道是否有某种方法可以在它发出时自动执行这些行
NETWORK_ERR: XMLHttpRequest Exception 101
在我粗略的想法中,我可能在myfile.js中有一些错误处理代码,或者在shell脚本文件中嵌入phantomjs myfile.js
,并捕获发生的错误
有人能教我怎么做吗
以下是我的主要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();
//myfile.js
var request=new XMLHttpRequest();
var myURLs=[“url1”、“url2”、…];//此数组包含超过10k个URL
对于(i=0;i叹气,我必须在这里回答我自己的问题
PhantomJS提供了一个名为onError
的全局错误处理程序。当PhantomJS抛出错误时,它非常有用
您可以在此处找到详细信息:请分享您的(复制所需的最低限度)PhantomJS代码和运行它的代码。@Vaviloff已经在我的问题正文中共享了我的最小代码。@Vaviloff我使用PhantomJS--web security=no myfile.js
10k URL运行代码?令人印象深刻。如果我可以问的话,用例是什么?@Vaviloff我需要检查我的服务器中是否可以访问这些10k电话号码。这些URL都是基于这些数字。