0

我有一个 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);
            }
        }
4

2 回答 2

1

我不完全了解您要做什么,但这不是您问题的解决方案吗?

if(responseData.substr(0,5) =="SMGFE")
于 2013-03-06T14:16:38.010 回答
1

您最好尝试从 responseData 中删除换行符和任何空格,或者在您的 php 文件中尝试

trim($your_php_variable_responseData);

请参阅php 手册

> This function returns a string with whitespace stripped from the beginning 
and end of str. Without the second parameter, trim() will strip these characters:

" " (ASCII 32 (0x20)), an ordinary space.
"\t" (ASCII 9 (0x09)), a tab.
"\n" (ASCII 10 (0x0A)), a new line (line feed).
"\r" (ASCII 13 (0x0D)), a carriage return.
"\0" (ASCII 0 (0x00)), the NUL-byte.
"\x0B" (ASCII 11 (0x0B)), a vertical tab.

或者您可以在 javscript 中尝试

responseData= responseData.replace(/^\s+|\s+$/g,"");
于 2013-03-06T14:23:18.800 回答