SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2011
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to check string equality inside inside of JQuery Ajax request's success block

    I'm trying to check the value inside a condition statement after it comes back from the server, but it doesn't seem to be working as expected. For debugging I'm checking the value with an alert statements and that value is what i'm expecting, but it doesn't seem to work inside of the condition statement.

    Code:
    $.ajax({
      type: "POST",
      url: "scripts/process-step1.php",
      data: $('.home1').serialize(),
      success: function(msg){
          alert(msg); // remove after testing
        
          if (msg === 'Life' ){
              document.location.href = 'form-life.php';
          }
          else{
             document.location.href = 'form.php';
          }        
           
      },
    I have tried both
    Code:
    msg === 'Life'
    Code:
    msg == 'Life'
    any ideas?

  2. #2
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,821
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    I am assuming that your alert already should you are getting the word Life ( capitalized) back from the AJAX request.
    As a test , to make sure a String type object is begin returned I would do :
    alert(typeof msg); // remove after testing


    Now, assuming msg is a string, remember that there could be BLANK spaces begin returned as well so you will want to do a trim before testing: msg=msg.trim();

    hope that helps

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2011
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    msg=msg.trim();
    trim method fixed it. Thanks!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •