【问题标题】:Connecting to Nagios Api via Website通过网站连接到 Nagios Api
【发布时间】:2016-07-18 07:18:52
【问题描述】:

我正在尝试通过网站连接到我的 Nagios 服务器,以显示 Nagios 服务器上主机的状态。

我已经运行了 curl 命令,我得到了一个 json 响应,它可以正常工作。但是现在当我尝试连接网络应用程序时,jQuery 给了我一个 GET 401 未经授权的错误。

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

我的代码:

function getData() {
  var name, code;
  var serviceURL = "http://test:welcome@41.87.218.55/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);
    }
  });
}

请让我知道我做错了什么。

【问题讨论】:

    标签: jquery json ajax api nagios


    【解决方案1】:

    这适用于遇到与我相同问题的任何人。

    nagios 服务器不接受我用来连接 API 的 Javascript 或 jQuery。这是因为同源政策。

    这是一个做同样事情的简单 PHP 脚本。

    <?php
    $url = 'http://test:welcome@41.87.218.55/nagios/cgi-bin/objectjson.cgi?query=hostlist&parenthost=none&childhost=none';
    $obj = json_decode(file_get_contents($url), true);
    
    print_r($obj);
    ?>
    

    希望对遇到同样问题的人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 2014-01-27
      • 2023-04-10
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多