weixin_33724046 2016-06-08 10:22 采纳率: 0%
浏览 22

如果还有关于ajax的结果

I am busy writing a web page for a company. I need to accomplish the following: If the output is nothing, alert "Rookswitch is turned off". If the output is "Hookswitch-Playlist", alert "Rookswitch is turned on". The data is grabbed from a 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");
        }
    });
});

For some reason, the message is always on. even if it is off. How can i fix this?

  • 写回答

2条回答 默认 最新

  • weixin_33712881 2016-06-08 10:31
    关注

    In your success function, you're doing an alert for "switched on" in all cases. No matter whether the response is blank, or contains a value.

    You've put the alert for "It's switched off" in the error function. This function will only be called if the AJAX request actually fails (i.e. there is a HTTP error).

    I think what you probably want to do is put an if/else block in your success function to check the value of the appropriate field in the response object to see if it contains the string "Hookswitch-Playlist" or not.

    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据