Home > Backend Development > PHP Tutorial > ajax返回字符串的比较,该怎么解决

ajax返回字符串的比较,该怎么解决

WBOY
Release: 2016-06-13 10:15:40
Original
907 people have browsed it

ajax返回字符串的比较

JScript code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->if(XMLHttp.readyState==4&&XMLHttp.status==200){    var a=XMLHttp.responseText;    a=a.replace(/\r\n/g,"");     var b="success";    if(a!=b)    {        document.getElementById("status").innerHTML=a;    }    else        eval("alert('添加成功');window.location='test2.php';");}
Copy after login

后台页面时echo "success";,返回的是"\r\nsuccess",然后经过正则替换后,成了"success",但是还是和b不相等,怎么回事呀,换成什么字符都是。

------解决方案--------------------
var a=XMLHttp.responseText; 这行下面加上:
alert(a); //弹出什么。贴出来。
------解决方案--------------------
看一下 a和b的length属性是否相同
如果照你说的 alert(a) 弹出的值显示的是 \r\nsuccess
那么它实际的内容应该是
JScript code
   var a = "\\r\\nsuccess";   var b = "success";<br><font color="#e78608">------解决方案--------------------</font><br>或者你可以用正则匹配来判断: <br>  var b=/success/g;<br>   if(!b.test(a))<br>   {<br>       document.getElementById("status").innerHTML=a;<br>   }<br>   else<br>       eval("alert('添加成功');window.location='test2.php';");<br><font color="#e78608">------解决方案--------------------</font><br>var a=XMLHttp.responseText;<br>alert('[' + a + ']');<br>说说都看到了什么<br><br>只有弄清楚了返回的是什么,才能判断是什么问题<br>瞎猜是没有意义的<br><div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template