Joe
Joe

Reputation: 1645

success callback

I'm using an ajax post like so...

$(document).on("click",".Resend",function() {

    $.ajax({
        type: "POST",
        url: "file.php",
        timeout: 3000,
        data: dataString,
        cache: false,
        success: function(myhtml){
            // If success
            if (myhtml == "success") {
                    alert(myhtml);
            } else {
                    alert("No");
            }
        }
    });
});

The PHP/HTML that is called back for example is a simple as this...

<?php
    echo "success";
?>

For some odd reason I am getting the false return alert = No. Can someone explain to me why this might happen?

Upvotes: 0

Views: 319

Answers (3)

Guillaume Poussel
Guillaume Poussel

Reputation: 9822

A JSON response format would avoid this kind of annoying issues with plain text.

Upvotes: 0

snjoetw
snjoetw

Reputation: 409

I answered a similar question here: Unable to compare two Strings in javascript

I think you might have the same problem.

Try do something like this in your success callback and see what the response text looks like:

alert('"' + myhtml + '"');

I'm suspecting there's leading or trailing space in your response. That might happen if there's space before your php starting or after the closing tag.

Upvotes: 0

Chris Hutchinson
Chris Hutchinson

Reputation: 9212

Assuming that the page you're posting to exists, it is possible that the contents of the response includes some white space; therefore, the string comparison would fail.

You can try this:

if ($.trim(myhtml) == 'success') {
  alert(myhtml)
}

Upvotes: 3

Related Questions