我有一个 ajax 代码如下。现在在我的 php 中的问题我只是写了一个回显作为代码,以防我的 sql 无法插入并且我尝试在此处进行比较,但我注意到它有额外的新行导致该语句失败 if(responseData=="SMGFE\n" )。我什至把多余的“\n”放在一起检查,但它也失败了。这个问题有什么解决办法吗?
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);
}
}