Karthika
Karthika

Reputation: 47

Response in Ajax

I have the following code segment.

ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var response = ajaxRequest.responseText;
            }
    };

How can I check whether 'response' is empty or not? Even though the function doesn't show any error, 'response' contains a not null value. My objective is to navigate to some other page if 'response' doesn't contain any error messages. Can you please tell me a solution?

Upvotes: 1

Views: 410

Answers (1)

Rob W
Rob W

Reputation: 348992

If you want to check whether the response is valid (ie, the page exists), check the status property:

if(ajaxRequest.status == 200) ; //... valid

If you want to check whether response is empty or not, test it in a condition response != "". Because an empty string evaluates to false in a logical expression, the != "" is not necessary.

if(ajaxRequest.responseText) ; //not empty

Upvotes: 4

Related Questions