Php 对if-else语句使用$.ajax()响应

Php 对if-else语句使用$.ajax()响应,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,我只是无法在if/else语句中使用ajax调用的响应。如果我在console.log()中输出响应,则表明它设置正确,但简单的If/else语句就是不起作用 function check_foo() { dataString = {action: 'do_this'}; $.ajax({ type: "POST", url: "ajax.php", data: dataString, success: function(foo) { conso

我只是无法在if/else语句中使用ajax调用的响应。如果我在console.log()中输出响应,则表明它设置正确,但简单的If/else语句就是不起作用

function check_foo() {
dataString = {action: 'do_this'};

$.ajax({
    type: "POST",
    url: "ajax.php",
    data: dataString,
    success: function(foo) {
        console.log('foo='+foo); // output: foo=ok

        if (foo=="ok") { // should be true, but isn't
           console.log('foo='+foo+' -> OK');
        } else { // instead this gets output
           console.log('foo='+foo+' -> FAIL'); // output: foo=ok -> FAIL
        }
    }
});
}

check_foo();
ajax.php:

echo 'ok'; // This is the result of the call
所以foo被正确地设置为“ok”(如console.log所示),但是if/else似乎无法识别它


任何帮助都将不胜感激

@johan评论对你来说是个完美的答案,我试过:-使用trim() function.js:-

function check_foo() {
dataString = {action: 'do_this'};

jQuery.ajax({
    type: "POST",
    url: "response.php",
    data: dataString,
    success: function(foo) {

        if (jQuery.trim(foo) == "ok") { 
           alert(foo);
        } else { 
          alert('jgjhg');
        }
    }
});
}
现在是ajax.php:-

if($_REQUEST['action'] == 'do_this') {
echo 'ok';
}

@johan评论对你来说是一个完美的答案,我试过这样做:-使用trim() function.js:-

function check_foo() {
dataString = {action: 'do_this'};

jQuery.ajax({
    type: "POST",
    url: "response.php",
    data: dataString,
    success: function(foo) {

        if (jQuery.trim(foo) == "ok") { 
           alert(foo);
        } else { 
          alert('jgjhg');
        }
    }
});
}
现在是ajax.php:-

if($_REQUEST['action'] == 'do_this') {
echo 'ok';
}

已尝试使用此代码

<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Stack Tester</title>
  <script src=jquery.min.js type="text/javascript"></script>
  <script type="text/javascript">
     //<![CDATA[
        function check_foo() {
            dataString = {action: 'do_this'};
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: dataString,
                success: function(foo) {
                    console.log('foo='+foo); // output: foo=ok
                    if (foo=="ok") { // should be true, but isn't
                        console.log('foo='+foo+' -> OK');
                    } else { // instead this gets output
                        console.log('foo='+foo+' -> FAIL'); // output: foo=ok -> FAIL
                    }
                }
            });
        }
     //]]>
  </script>
</head>
<body>
  <script type="text/javascript">
      //<![CDATA[
        check_foo();
      //]]>
  </script>   
</body>
</html>

堆栈测试仪
//OK');
}else{//取而代之的是获取输出
console.log('foo='+foo+'->FAIL');//输出:foo=ok->FAIL
}
}
});
}
//]]>
//
使用ajax.php

<?php
echo 'ok';

尝试使用此代码

<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Stack Tester</title>
  <script src=jquery.min.js type="text/javascript"></script>
  <script type="text/javascript">
     //<![CDATA[
        function check_foo() {
            dataString = {action: 'do_this'};
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: dataString,
                success: function(foo) {
                    console.log('foo='+foo); // output: foo=ok
                    if (foo=="ok") { // should be true, but isn't
                        console.log('foo='+foo+' -> OK');
                    } else { // instead this gets output
                        console.log('foo='+foo+' -> FAIL'); // output: foo=ok -> FAIL
                    }
                }
            });
        }
     //]]>
  </script>
</head>
<body>
  <script type="text/javascript">
      //<![CDATA[
        check_foo();
      //]]>
  </script>   
</body>
</html>

堆栈测试仪
//OK');
}else{//取而代之的是获取输出
console.log('foo='+foo+'->FAIL');//输出:foo=ok->FAIL
}
}
});
}
//]]>
//
使用ajax.php

<?php
echo 'ok';

是否触发了
else
命令?您确定响应中没有额外的空间吗?你试过修剪它吗?你确定数据中只有
'ok'
而已。我刚试过你的代码,效果很好。我得到了“foo=ok->ok”
if($.trim(foo)==“ok”)
是否触发了
else
响应?您确定响应中没有额外的空间吗?你试过修剪它吗?你确定数据中只有
'ok'
而已。我刚试过你的代码,效果很好。我得到了“foo=ok->ok”
if($.trim(foo)==“ok”)
Perfect,减去
alert
语句。为了大家的理智,学习使用
console.log
。就是这样-非常感谢!我只是不知道为什么-console.log('foo=*'+foo+'*');显示除
alert
语句外,没有其他空格sperfect。为了大家的理智,学习使用
console.log
。就是这样-非常感谢!我只是不知道为什么-console.log('foo=*'+foo+'*');显示没有其他空间