6

I have a problem with my jquery ajax. I have this code:

$.ajax({
    url: '/users/validatepassword/'+current,
    success: function(data){
        status = data;
    },
    async: false
});

if(status == "Password correct")
{
    //do something
}

Basically, I want to capture the "data" that was returned on "success". But I cannot make a way for the if statement to work. I am think the "data" was not a string so I cannot make a comparison.

7
  • 1
    have you checked the data returning, try alert(data); first Commented Jul 27, 2012 at 8:21
  • @nbrooks He has async: false. But comebal - you probably shouldn't code that way. Commented Jul 27, 2012 at 8:25
  • @Ariel Wow, completely missed that lol Commented Jul 27, 2012 at 8:26
  • @comebal can you show the value for data...I think data is in some other format like JSON or something else Commented Jul 27, 2012 at 8:28
  • @Ariel so should I take it away? I'm just a beginner so I don't really get things yet. Commented Jul 27, 2012 at 8:38

5 Answers 5

6

Define status outside ajax call. then access that everywhere.

var status = '';
$.ajax({
    url: '/users/validatepassword/'+current,
    async: false,
    dataType: "json",
    success: function(data){
        status = data;
    },

});

if(status == "Password correct")
{
    //do something
}

At users/validatepassword use json_encode()

echo json_encode("Password correct");
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. You actually solved my problem. I did some little modifications and it worked. >:D<
@comebal: I'm glad that I was able to help.
Tip: use dataType: "text" or dataType: "xml" for TXT or for XML.
4

Try status checking condition inside the ajax code.

$.ajax({
    url: '/users/validatepassword/'+current,
    success: function(data){
        status = data;
        if(status == "Password correct")
        {
         //do something
        }
    },
    async: false
});

if the condition is outside ajax, It will execute before the ajax return.

Comments

2

You can try like this,

 var response = $.ajax({
            url: '/users/validatepassword/'+current,
            async: false
        }).responseText;

        if(response == "Password correct")
        {
            //do something
        }

Comments

1

You see you should process the message within the success function not from outside.

var status = '';
$.ajax({
    url: '/users/validatepassword/'+current,
    async: false,
    success: function(data){
        status = data;
        if(status == "Password correct")
        {
            //do something
        }
    }
});

Comments

1

@Comebal: Buddy this is what u need to do:

Firstly remove the async:false

$.ajax({
    url: '/users/validatepassword/'+current,
    success: function(data){
       //alert(data); 
       if(data == "Password correct")
        {
                  //do something
        }
    }
});

Then, the major part is make sure data from ajax page is "Password correct" or else u can't "do something"... :)

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.