我正在尝试使用ajax调用在nagios中安排服务的停机时间。我可以通过NAgios GUI安排它,并找到一种方法来使用curl进行调度。
我找到了一个链接How to set downtime for any specific nagios host for certain time from commandline through curl?,它解释了如何通过curl命令实现它。
我试图通过curl命令实现它。
curl \
--data cmd_typ=56 \
--data cmd_mod=2 \
--data host=jenkins \
--data "service=Jenkins+PROD+GUI" \
--data "com_author=Nagios Admin"\
--data "com_data=Test" \
--data trigger=0 \
--data "start_time=05-09-2018+14%3A05%3A14" \
--data "end_time=05-09-2018+16%3A05%3A14" \
--data fixed=1 \
--data btnSubmit=Commit \
http://xx.xx.xx:8087/nagios/cgi-bin/cmd.cgi -u "nagiosadmin:nagiosadmin"
工作正常。
我尝试将相同的curl功能转换为ajax post call。
HTML :
<form name="NAME" id="avialform" class="avail" action="">
<fieldset id="availfield">
<legend style="color:white" id="availegend">SCHEDULED DOWNTIME</legend>
<table width="100%" cellpadding="0" cellspacing="0" class="vzuui-detailpanel">
<tr>
<td><label>Service :</label>
<select id = "ServiceList">
<option value = "Jenkins+PROD+GUI">Jenkins Prod</option>
</select>
</td>
</tr>
<tr>
<td><label>From Date :</label><input id="from" type="datetime-local" name="fromdate" /></td>
</tr>
<tr>
<td><label>To Date :</label><input id="to" type="datetime-local" name="todate" /></td>
</tr>
<tr>
<td><label>Comment :</label><input id="comment" type="text" name="Servicecommt" /></td>
</tr>
</table>
</fieldset>
<button class="vzuui-btn-red-active" type="button" id="getrepo">Submit</button>
</form>
Ajax:
var posdata = {"cmd_typ":56,"cmd_mod":2,"host":"jenkins","service":"Jenkins+PROD+GUI","com_author":"Nagios Admin","com_data":"Test","trigger":0,"start_time":"2018-05-09T18:00","end_time":"2018-05-09T19:00","fixed":1,"btnSubmit":"Commit"}
posdata["service"] = select.options[select.selectedIndex].value;
posdata["com_data"] = document.getElementById("comment").value;
posdata["start_time"] = document.getElementById("from").value;
posdata["end_time"] = document.getElementById("to").value;
console.log(JSON.stringify(posdata));
$.support.cors = true;
$.ajax({
url: "http://xx.xx.xx:8087/nagios/cgi-bin/cmd.cgi",
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization',
make_base_auth("nagiosadmin", "nagiosadmin"));
},
type: 'POST',
dataType: 'json',
contentType: 'application/json',
processData: false,
data: posdata,
success: function (data) {
alert(JSON.stringify(data));
},
error: function(){
alert("Cannot get data");
}
});
但是我收到了500内部服务器错误。请指导我使用ajax实现这一目标。
答案 0 :(得分:1)
似乎数据应该作为表单而不是json发送。删除contentType: 'application/json'
,它应该可以正常工作
contentType(默认值:'application / x-www-form-urlencoded; charset = UTF-8')
类型:布尔值或字符串
将数据发送到服务器时,请使用此内容类型。默认为“application / x-www-form-urlencoded;
http://api.jquery.com/jquery.ajax/
Edit-1:09-May-2018
您应该按照以下更新代码
var posdata = {
"cmd_typ": 56,
"cmd_mod": 2,
"host": "jenkins",
"service": "Jenkins+PROD+GUI",
"com_author": "Nagios Admin",
"com_data": "Test",
"trigger": 0,
"start_time": "2018-05-09T18:00",
"end_time": "2018-05-09T19:00",
"fixed": 1,
"btnSubmit": "Commit"
}
posdata["service"] = select.options[select.selectedIndex].value;
posdata["com_data"] = document.getElementById("comment").value;
posdata["start_time"] = document.getElementById("from").value;
posdata["end_time"] = document.getElementById("to").value;
console.log(JSON.stringify(posdata));
$.support.cors = true;
$.ajax({
url: "http://xx.xx.xx:8087/nagios/cgi-bin/cmd.cgi",
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization',
make_base_auth("nagiosadmin", "nagiosadmin"));
},
type: 'POST',
data: posdata,
success: function(data) {
alert(JSON.stringify(data));
},
error: function() {
alert("Cannot get data");
}
});
将确保数据为application/x-www-form-urlencoded
并且jQuery还会检查响应并确定类型