如何设置 ajax responseText 等于字符串?
How do I set ajax responseText to be equal to string?
我在使用 ajax.responseText 时遇到问题,它必须等于从 php 外部文件回显传入的字符串,
问题是字符串似乎不等于 ajax.responseText,
但如果 alert(ajax.responseText) 它正是字符串 "friend_request_sent"
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);
}
现在我从 friend_system.php
获取代码
...{
echo"friend_request_sent";
exit();
}
和ajax js代码
function ajaxObj( meth, url ) {
var x = new XMLHttpRequest();
x.open( meth, url, true );
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState == 4 && x.status == 200){
return true;
}
}
第三个条件是它挣扎的地方,它总是去别的地方,为什么?
要根据任何字符串检查 ajax 响应,最好是 trim 响应文本
request.responseText.trim() === "string"
我在使用 ajax.responseText 时遇到问题,它必须等于从 php 外部文件回显传入的字符串, 问题是字符串似乎不等于 ajax.responseText,
但如果 alert(ajax.responseText) 它正是字符串 "friend_request_sent"
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);
}
现在我从 friend_system.php
获取代码 ...{
echo"friend_request_sent";
exit();
}
和ajax js代码
function ajaxObj( meth, url ) {
var x = new XMLHttpRequest();
x.open( meth, url, true );
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState == 4 && x.status == 200){
return true;
}
} 第三个条件是它挣扎的地方,它总是去别的地方,为什么?
要根据任何字符串检查 ajax 响应,最好是 trim 响应文本
request.responseText.trim() === "string"