2012-05-01 32 views
0

我有这段代码:jQuery的POST数据回报比较不工作

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回声的“X”,如果事情是错误的。

我想比较从reminme.php的输出,但即使它回声的x,条件data == "x"不起作用。

我加入alert(data),我可以看到它正确显示需要的时候x ..

+0

尝试'警报( ' '+ X +''“)'。任何空格? – ori

+0

@ori nope,没有空格。返回“x” –

+0

“alert(typeof data)”的输出是什么;'? –

回答

1

如果服务器相呼应的x字符串,则成功回调内部if (data == 'x')测试应该工作。你的问题在别的地方。

1

也许已经晚了。我面临着同样的问题,我解决了非常愚蠢的解决方法(适用于您的代码):

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

我不知道为什么:/

希望它能帮助。

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; 
    }