0

I'm trying to compare the result from from ajax call with a string. My result is returning the correct result but i can't get the if statement to compare this with my string, any ideas?

      $.ajax({
        type: "POST",
        url: "sessionCheck.php",
        data: {id:mwdata},
        success: function(html) {
            if (html == "open"){
                alert ("yes it's open");   
            }else if(html == "closed"){
                alert ("no it's closed");   
            }  
        }
      });
5
  • 1
    Are you getting some kind of error? What do you see when you add console.log(html)? Commented Jan 21, 2019 at 14:06
  • Odds are that html contains whitespace you aren't accounting for. Use JSON instead of invalid HTML. Commented Jan 21, 2019 at 14:08
  • I don't get any errors and the console.log just confirms i'm getting the result of either open or closed Commented Jan 21, 2019 at 14:11
  • Are you sure there is no whitespace in your response? Run the code: console.log(html.length); and see the length is correct Commented Jan 21, 2019 at 14:54
  • Another way is to add this code: else { alert("it is not close neither open"); } Commented Jan 21, 2019 at 14:54

1 Answer 1

1

So the answer was to trim the result var result = $.trim(html);

Sign up to request clarification or add additional context in comments.

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.