As a newcomer to JavaScript and PhantomJS, I have been encountering an issue when running myfile.js (which involves for
loops) with the command phantomjs myfile.js
. It consistently throws the error:
NETWORK_ERR: XMLHttpRequest Exception 101: A network error occurred in synchronous requests.
Each time this error occurs, I need to stop the process (using Ctrl+C in my terminal) and then execute the following bash commands to resume my work:
$ some bash command to clean the job done before error occurs
$ phantomjs myfile.js
This situation is quite frustrating, especially when dealing with a large for
loop. I am now seeking a way to automatically run these lines whenever the mentioned error is encountered.
In considering possible solutions, I am thinking of implementing error handling code within myfile.js or incorporating phantomjs myfile.js
into a shell script file to capture the occurrence of the error.
If anyone could provide guidance on how to achieve this, I would greatly appreciate it.
Here is a snippet of my main PhantomJS code:
// 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();
Due to privacy reasons, I am unable to disclose the URLs within the myURLs
array. I apologize for this limitation but still welcome any insights or suggestions based on the provided code snippet.