1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| /*
Function httpRequest:
- Method: GET
- URL: Handles remote URL
- Handles Login/password
- responseFormat: Return the response of the request in the desired format (XML or Text, default Text)
- Waits for the end of the request
*/
function httpRequest(method, url, username, password, responseFormat){
// Defining XMLHttpRequest element
if(window.XMLHttpRequest){ // Firefox, Opera, Safari
var xhr_object = new XMLHttpRequest();
// Enable remote access for XMLHttpRequest
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
}else{ // Other browser
alert("Your browser does not support XMLHttpRequest, try Firefox.");
return;
}
// Preparing request
if(method == "GET"){ // The method is allowed
xhr_object.open(method,url, false, username, password);
}else{
alert("Incorrect method. The correct methods are: GET, POST.");
return;
}
// Sending request
xhr_object.send(null);
if(xhr_object.readyState == 4){
if(xhr_object.status == 200){
if(responseFormat == "XML"){
return xhr_object.responseXML.documentElement;
}else{
return xhr_object.responseText;
}
}else{
alert("HTML status code error, check again username/password.");
return;
}
}else{
alert("Request didn't complete, check again URL.");
return;
}
}
function main(){
// Loading the Nagios Telnet Availability Report
var telnetAvailabilityReport = httpRequest("GET", "http://www.monurl.com", "user", "pass", "Text");
alert(getElementsByClass("serviceOK",telnetAvailabilityReport,"td")[2])); |
Partager