jQuery'If'语句字符串比较不工作 [英] jQuery 'If' statement string comparison not working

查看:353
本文介绍了jQuery'If'语句字符串比较不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常努力的时间试图做一些非常简单的事情。下面是代码:

I'm having a very hard time trying to do something very simple. Here's the code:

        if(data == 'success') {

            alert('foo');

        } else {

            alert(data);

        }

我已经简化了它,但这是必要的了解发生了什么。变量data是AJAX调用的结果,如果这有什么区别。问题是,它总是去else语句,它警告成功,如果它到'else'它不应该。任何想法这里发生了什么?

I've simplified it, but that's all that's necessary to understand what's going on. the variable 'data' is a result of an AJAX call, if that makes any difference. The problem is that it always goes to the 'else' statement and it alerts 'success', which it shouldn't if it goes to the 'else'. Any idea what's going on here?

编辑:这是jQuery中的完整的AJAX代码:

Here's the full AJAX code in jQuery:

$.post("/manage_sites.php", {before:before, edit:after}, function(data){

        if(data == success) {

            alert('blah');

        } else {
            alert(data);
        }
    });

然后在PHP响应中:

...code....
$update = mysql_query("UPDATE users SET feeds = '$afterFeed' WHERE username = '$name'") or     die("Query Failed");

if($update) {

    echo  'success';  //this is the 'string' that is being given to 'data'
}


推荐答案

您可以使用 $。trim() 如下:

You can fix it on the client side using $.trim() like this:

if($.trim(data) == 'success') {

线来自服务器端,可能是您的PHP中某个错误的新行,检查<?之前或之后

Or, a better approach would be removing the new-line coming from the server-side, probably an erroneous new-line in your PHP somewhere, check before or after the <? ?> block, this is most often where they crop up.

或者,在输出内容后退出,如下所示:

Or, just exit after outputting your content, like this:

if($update) {
  echo 'success';
  exit();
}

这篇关于jQuery'If'语句字符串比较不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆