I am trying to connect to my Nagios server through a website to display to state of the hosts on the Nagios server.
I have run the curl command and I get a json response back and it works. But now when I try to connect for the web app it jQuery gives me a GET 401 unauthorized error.
jquery-3.1.0.js:9392 GET http://41.87.218.55/nagios/cgi-bin/objectjson.cgi?query=hostlist&parenthost=none&childhost=none 401 (Unauthorized)
send @ jquery-3.1.0.js:9392
ajax @ jquery-3.1.0.js:8999
getData @ myJavaAng.js:187
(anonymous function) @ myJavaAng.js:203
My code:
function getData() {
var name, code;
var serviceURL = "http://test:[email protected]/nagios/cgi-bin/objectjson.cgi?query=hostlist&parenthost=none&childhost=none";
$.support.cors = true;
$.ajax({
type: "GET",
dataType: "json",
crossDomain: true,
url: serviceURL,
success: function(data) {
alert(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(errorThrown);
}
});
}
Please let me know what I'm doing wrong.
This is for anyone who has the same problem I did.
The nagios server was not accepting the Javascript or jQuery that I was using to connect to the API. This is because of a Same-origin Policy.
Here is a simple PHP script that does the samething.
<?php
$url = 'http://test:[email protected]/nagios/cgi-bin/objectjson.cgi?query=hostlist&parenthost=none&childhost=none';
$obj = json_decode(file_get_contents($url), true);
print_r($obj);
?>
Hope this helps anyone who has the same problem.