1

I just can't get it to work that I'm using the response of an ajax call in an if/else statement. If I output the response in console.log() it shows that it is correctly set, but the simple if/else statement just doesn't work.

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

so foo is correctly set as 'ok' (as shown in console.log), but the if/else doesn't seem to recognize it...

Any help would be very much appreciated!

11
  • 1
    Does the else fire? Commented Jul 5, 2013 at 13:00
  • 5
    Are you sure there is no additional space in the response ? Did you try to trim it ? Commented Jul 5, 2013 at 13:01
  • 1
    You are sure that there is no more than just 'ok' in the data. Commented Jul 5, 2013 at 13:01
  • 1
    I just tried your code, it works fine. I got "foo=ok -> OK" Commented Jul 5, 2013 at 13:04
  • 2
    if ($.trim(foo)=="ok") Commented Jul 5, 2013 at 13:04

2 Answers 2

4

@johan comment is perfect answer for you i have tried it like:- use 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');
        }
    }
});
}

now ajax.php:-

if($_REQUEST['action'] == 'do_this') {
echo 'ok';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, minus the alert statements. For everyone's sanity, learn to use console.log.
That was it - thanks a lot! I just don't know why - console.log('foo=*'+foo+'*'); shows me there are no additional spaces
0

Tried with this code

<!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>

with ajax.php

<?php
echo 'ok';

and it worked fine.

version of jquery.min.js is 1.7.1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.