php的Ajax结果有额外的新行
我有一个ajax代码,如下所示。现在在我的php中,我只写了一个echo作为代码,以防我的sql插入失败,我尝试在这里进行比较,但我注意到它有额外的行,如果(responseData==“SMGFE\n”),它会导致该语句失败。我甚至将额外的“\n”放在一起进行检查,但它也失败了。这个问题有什么解决办法吗php的Ajax结果有额外的新行,php,ajax,Php,Ajax,我有一个ajax代码,如下所示。现在在我的php中,我只写了一个echo作为代码,以防我的sql插入失败,我尝试在这里进行比较,但我注意到它有额外的行,如果(responseData==“SMGFE\n”),它会导致该语句失败。我甚至将额外的“\n”放在一起进行检查,但它也失败了。这个问题有什么解决办法吗 function ajaxLoad(url,postData,plain) { //alert("in url : "+url); var ht
function ajaxLoad(url,postData,plain) {
//alert("in url : "+url);
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType && plain) {
http_request.overrideMimeType('text/plain');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = function() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var responseData = http_request.responseText;
alert("http response :"+responseData+"TEST");
if(responseData=="SMGFE\n")
{
alert("Gname "+document.getElementById("gname").value+" Already Exist");
}
else{
alert("Successfully Inserted");
clearSelection();
window.opener.location.reload();
window.close();
}
}
else {
alert('Request Failed: ' + http_request.status);
}
}
};
//alert("before post data"+postData.length);
if (postData) { // POST
//alert("post data"+postData.length);
http_request.open('POST', url, true);
http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
http_request.setRequestHeader("Content-length", postData.length);
http_request.send(postData);
}
else {
http_request.open('GET', url, true);
http_request.send(null);
}
}
我不太明白你想做什么,但这不是解决你问题的办法吗
if(responseData.substr(0,5) =="SMGFE")
您最好尝试删除responseData中的换行符和任何空白,或者在php文件中尝试
trim($your_php_variable_responseData);
见
或者你也可以试试这个脚本
responseData= responseData.replace(/^\s+|\s+$/g,"");
我尝试了你的javascript方法,它正在工作。但是为什么echo有额外的\n原因,或者这是正常的行为?它肯定不是正常的行为,但是没有看到php代码,很难(不可能)说出是什么导致了它
responseData= responseData.replace(/^\s+|\s+$/g,"");