0

以下のようなajaxコードがあります。私のPHPの問題は、SQLが挿入に失敗した場合にコードとして1つのエコーを書くだけで、ここで比較しようとしましたが、新しい行が余分にあることに気づきました。これにより、このステートメントが失敗します 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

php ファイルで、responseData から改行と空白を削除することをお勧めします。

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 に答える