ajax同步模式的浏览器兼容有关问题

WBOY
發布: 2016-06-13 13:38:02
原創
1116 人瀏覽過

ajax同步模式的浏览器兼容问题
upload.php

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->



<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>



登入後複製
f
<script> function GetXmlHttpObject() { var XMLHttp=null; try { XMLHttp=new XMLHttpRequest(); } catch(e) { try { XMLHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { XMLHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return XMLHttp; } function upload() { var flag=false; var XMLHttp=GetXmlHttpObject(); var url="upload_deal.php"; var dataStr="hour="+document.getElementById("hour").value; XMLHttp.open("POST",url,false); XMLHttp.setRequestHeader("content-type","application/x-www-form-urlencoded"); XMLHttp.send(dataStr); var resTest=XMLHttp.responseText; resTest=resTest.trim(); var response_compent="true"; if(resTest!=response_compent) { document.getElementById("status").innerHTML=resTest; } else flag=true; return flag; } </script>


后台upload_deal.php
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
    if($_REQUEST['hour']==1)
    {    
        echo "true";
    }
    else
    {
        echo "false";
    }

登入後複製


想实现将hour提交到后台判断,再根据返回值,true正确则提交表单、跳转,false错误则不提交表单、不跳转、给出提示。但是这个代码在firefox和谷歌等非ie内核的浏览器没有问题,但是使用ie(我这是9)就全部都跳转了。尝试加上回调函数的状态判断onreadystatechange,但是所有浏览器就都不执行了,一过onreadystatechange就直接到最后的return了。
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
        XMLHttp.onreadystatechange=function()
        {
            if(XMLHttp.readyState==4&&XMLHttp.status==200)
            {
                var resTest=XMLHttp.responseText;
                resTest=resTest.trim(); 
                var response_compent="true";
                if(resTest!=response_compent)
                {
                    document.getElementById("status").innerHTML=resTest;
                }
                else
                    flag=true;
                
            }
        }

登入後複製


------解决方案--------------------
搜了搜,javascript没有trim()方法吧? resTest=resTest.trim();
------解决方案--------------------
探讨

引用:

搜了搜,javascript没有trim()方法吧? resTest=resTest.trim();

这个还真有,不是这的问题
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!