在开发中需要ajax调用zabbix-api,报错误码-32700,服务器接收到无效的JSON。原因是json数据发送到后台,后台无法解析,
错误请求如下:
$.ajax({
type:"post",
dataType : "json",
contentType:"application/json",
url: 'https://192.168.1.165/zabbix/api_jsonrpc.php',
data:{
"jsonrpc": "2.0",
"method": "script.create",
"params": {
"name": "001",
"command": "zabbix_get -s 192.168.1.10 -p 10050 -k check_remote",
"host_access": 3,
"confirmation": "Are you sure you would like to reboot the server?"
},
"auth": response,
"id": 10
},
success: function (data) {
let result={"scriptids":data.result.scriptids[0],"auth":response,'ipStr':ipStr}
console.log("第二层脚本创建成功"+JSON.stringify(result))
resolve(result);
},
error: function (err) {
console.log("第二层失败"+err);
}
})
正确传参方式是将json变成字符串传给data
data:JSON.stringify({
"jsonrpc": "2.0",
"method": "script.create",
"params": {
"name": name,
"command": "zabbix_get -s" +" "+ipStr+" "+ "-p 10050 -k check_remote",
"host_access": 3,
"confirmation": "Are you sure you would like to reboot the server?"
},
"auth": response,
"id": 10
}),