jQuery post数据返回比较不起作用

时间:2012-05-01 18:52:25

标签: javascript jquery

我有这段代码:

function addEmail() {
            email = $("#email").val();
            atime = $("select#time").val();
            tid = <?= $tid; ?>;
            $.post("remindme.php", { email: email, time: atime, tid: tid },
                function(data) {
                    if (data == "x") {
                        alert(data);
                        $('#remindme').modal('hide');
                        $('#quota').show();
                    }
                    else {
                        alert(data);
                        $('#remindme').modal('hide');
                        $('#remindersuccess').show();
                    }
                });
            }
如果出现问题,

remindme.php echo的“x”。

我正在尝试比较remindme.php的输出,但即使它回显了x,条件data == "x"也不起作用。

我添加了alert(data),我可以看到它在需要时正确显示x ..

3 个答案:

答案 0 :(得分:1)

如果服务器回显x字符串,则成功回调中的if (data == 'x')测试应该有效。你的问题出在其他地方。

答案 1 :(得分:1)

也许是迟到了。我遇到了同样的问题,我解决了非常愚蠢的解决方法(适用于您的代码):

function(data) {
    var server_info = data;
    if (server_info == "x") {                       
         $('#remindme').modal('hide');
          $('#quota').show();
    } else {
         $('#remindme').modal('hide');
         $('#remindersuccess').show();
    }
}

我不明白为什么:/

希望它有所帮助。

答案 2 :(得分:1)

我也许遇到同样的问题,我这样解决了:

    var verify = function (user,fName,fPass) {
        var result = "";
        $.ajaxSetup({async:false});
        $.post("php_scripts/verify"+user+".php",{login:fName, pass:fPass},function (data) {
            result = $.trim(data);   // use $.trim when are you using "data"
        });
        return result;
    }
相关问题