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.

    评论

报告相同问题?