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
        }else{
            // Inform of failure
        }
    }
}
ajax.send("testing=1");

that pairs with the following PHP:

if(isset($_POST["testing"])){
    $formScore = checkForm($db_conx);
    if($formScore == 10){
        echo "success";
        exit();
    }else{
        echo "fail";
        exit();
    }
}

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:

$('.output').html(ajax.responseText);

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.

查看全部
weixin_33729196
weixin_33729196
2015/01/12 08:26
  • php
  • ajax
  • javascript
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信

2个回复