如何将ajax responseText设置为等于字符串?

时间:2018-12-28 22:03:33

标签: javascript php ajax responsetext

我的ajax.responseText有问题,条件是它必须等于从外部文件的php echo传入的字符串, 问题是字符串似乎不等于ajax.responseText,

但是如果alert(ajax.responseText)正是字符串“ friend_request_sent”

nojoin=False

现在是从friend_system.php获取的代码

function friendToggle(type,user,elem){
var conf = confirm("Press OK to confirm the '"+type+"' action for user <?php echo $u; ?>.");

if(conf != true){
    return false;
}   
_(elem).innerHTML = 'please wait ...';
var ajax = ajaxObj("POST", "friend_system.php");

ajax.onreadystatechange = function() {
    if(ajaxReturn(ajax) == true) {

        if(ajax.responseText=="friend_request_sent"){          
            _(elem).innerHTML = 'OK Friend Request Sent';       
        } else if(ajax.responseText == "unfriend_ok"){
            _(elem).innerHTML = '<button onclick="friendToggle(\'friend\',\'<?php echo $u; ?>\',\'friendBtn\')">Request As Friend</button>';       
        } else{        
            alert(ajax.responseText);
            _(elem).innerHTML = 'Try again later';
        }
    }
}
ajax.send("type="+type+"&user="+user);

}

和ajax js代码

 ...{ 
 echo"friend_request_sent";
 exit();
 }

} 第三个条件是它在哪里挣扎,它总是在其他地方挣扎,为什么?

1 个答案:

答案 0 :(得分:0)

要检查针对任何字符串的ajax响应,最好修剪响应文本

request.responseText.trim() === "string"