WAMP 和 Ajax 查询的奇怪行为


Strange behaviour with WAMP and an Ajax query

我目前正在尝试运行一个脚本,该脚本适用于我的 LAMP 环境,但不适用于 WAMP 环境:

$.ajax(
        {
            url: '<?php echo ROOT_DIR;?>/member/login-process',
            type: "post",
            data: $('form').serialize(),
            success: function(data)
            {
                if (data == 'success')
                {
                    setTimeout(function(){window.location.href = '<?php echo ROOT_DIR;?>/dashboard';}, 2000);
                }
                else
                {
                    $("#alert").html('<div class="alert alert-error"><i class="icon-exclamation-sign"></i> '+data+'</div>');
                }
            }
        });

当我在 LAMP 上尝试它时,它工作正常:我被重定向了。对于WAMP,我不知道为什么,但是我有以下错误消息(由"else"生成):

成功

我已经尝试了typeof(data),结果是"字符串"。因此,Ajax 查询返回的值是"成功"字符串,那么为什么忽略"if"呢?

试试这个:

var go = $.ajax({
    type: 'POST',
    url: '<?php echo ROOT_DIR;?>/member/login-process',
    data: $('form').serialize()
})
.done(function(data) {
    console.debug("DATA:");
    console.debug(data);
    if (data == 'success')
    {
        setTimeout(function(){window.location.href = '<?php echo ROOT_DIR;?>/dashboard';}, 2000);
    }
    else
    {
        $("#alert").html('<div class="alert alert-error"><i class="icon-exclamation-sign"></i> '+data+'</div>');
    }
})
.fail(function(msg) {
    alert('Error: ' + msg);
})
.always(function() {
});

success 一直是 jQuery 中成功回调的传统名称,定义为 ajax 调用中的一个选项。但是,由于实现了$.Deferred和更复杂的回调,因此done是实现成功回调的首选方法,因为它可以在任何延迟时调用。