我正忙着为公司写一个网页。我需要完成以下操作:如果输出为空,alert "Rookswitch is turned off"
。如果输出为"Hookswitch-Playlist"
,alert "Rookswitch is turned on"
。
数据来自REST API。
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://192.168.0.15:8080/Cnario/REST/GetDevice?deviceName=Rook-Switch",
dataType: "json",
processdata: true,
success: function(response) {
alert("De Rookswitch staat al aan");
if (response.GetDeviceResult.DevicePins.PinValue == 'Playlist-Roken')
handleSuccess(response);
else
handleError(response);
},
error: function(message) {
alert("Rookswitch is uit");
}
});
});
由于某种原因,该消息始终打开。即使它关闭了。我该如何解决这个问题?
答案 0 :(得分:0)
在您的成功功能中,您在所有情况下都会“开启”警报。无论响应是空白还是包含值。
您已在错误功能中设置“已关闭”警报。只有在AJAX请求实际失败时才会调用此函数(即存在HTTP错误)。
我认为您可能想要做的是在成功函数中放置一个if / else块来检查响应对象中相应字段的值,以查看它是否包含字符串“Hookswitch-Playlist”。 / p>
答案 1 :(得分:0)
如果您能够控制http://192.168.0.15:8080/Cnario/REST/GetDevice?deviceName=Rook-Switch响应,我强烈建议您使用类似
的内容/* --------------------------------------- */
/* Response will be generated according to which
/* prog lang you are using but as a result we should have sth like this
/* --------------------------------------- */
var response = {
code: 200, // on
message: 'Rookswitch is turned on'
}
/* OR */
var response = {
code: 300, // off
message: 'Rookswitch is turned off'
}
/* --------------------------------------- */
/* Response
/* --------------------------------------- */
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://192.168.0.15:8080/Cnario/REST/GetDevice?deviceName=Rook-Switch",
dataType: "json",
processdata: true,
success: function(response) {
alert(response.message);
/* --------------------------------------- */
/* doing sth according to server response
/* --------------------------------------- */
if(response.code == 200){
/* On function goes here */
}else if(response.code == 300){
/* Off function goes here */
}
},
error: function(message) {
alert("Rookswitch is uit");
}
});
});