AJAX responseText不匹配

I have an AJAX call:

var ajax = new XMLHttpRequest();
ajax.open("POST", "testing.php", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.onreadystatechange = function(){
    if(ajax.readyState == 4 && ajax.status == 200){
        var returnVal = ajax.responseText;

        if(returnVal == "success"){
            // Redirect
            // Inform of failure

that pairs with the following PHP:

    $formScore = checkForm($db_conx);
    if($formScore == 10){
        echo "success";
        echo "fail";

And this works just fine. The form score is "10" when it should be, and the value sent back to the AJAX call is, in fact, "success". At least, if I print out the response text:


the value in output is " success" - WITH the space preceding the word. I cannot select the space with my cursor, but it is there when I view the HTML for the page.

But, no matter what, the line if(returnVal == "success") always equates to false. I've even tried if(returnVal == " success"), but still false.

This is all the code. I've even tried hard-coding $formScore = 10; to see if that was the issue. Regardless, for some reason the equality always comes out false.

Is there something obvious I'm missing? I've tested this 100 different ways, and still the same result.

2015/01/12 08:26
  • php
  • ajax
  • javascript
  • jquery
  • 点赞
  • 收藏
  • 回答