初学web,XMLHttpRequest对象搞得我好乱,在IE和非IE总不能实现正确使用。
html代码如下:
<html>
<head>
<title>用户注册</title>
<script language="javascript" type="text/javascript">
<!--
//定义用于存储XMLHttpRequest对象的变量
var xmlHttp = null;
var isIe=(document.all)?true:false;
//用于创建XMLHttpRequest对象的函数
function creatXmlHTTP()
{
//if(window.ActiveXObject)
if(isIe)
{
var arrActiveX = ['Microsoft.XMLHTTP','MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP'];
for(var i=0;i<arrActiveX.length;i++)
{
try
{
xmlHttp = new ActiveXObject(arrActiveX[i]);
break;
}
catch(ex)
{
}
}
}//else if(window.XMLHttpRequest){
else{
xmlHttp = new XMLHttpRequest();
}
} //用于响应XMLHttpRequest对象状态变化的函数
function checkData()
{
//判断异步调用是否完成
if (xmlHttp.readyState==4)
{
//判断异步调用是否成功,为了便用没有HTTP服务器读者的测试,加上了“xmlHttp.status==0”的条件,如果在服务器中运行,可以不用该条件
if (xmlHttp.status==200 || xmlHttp.status==0)
{
//以文本形式返回服务器中的数据
var userNames = xmlHttp.responseText;
//将userName.txt文本中的字符串分割成数组
var arrUserName = userNames.split("、");

//用于判断用户名是否已经存在的变量
var bFlag = false;

for (i=0;i<arrUserName.length;i++)
{
if (arrUserName[i]==myForm.username.value)
{
//如果用户名已经存在,变量bFlag设为true
bFlag = true;
break;
}
}

//查找用于显示结果的节点
var node = document.getElementById("userInfo");
if (bFlag)
{
node.firstChild.nodeValue = "用户名已经存在,请重新输入";
document.getElementById("mySubmit").disabled = true;
}
else
{
node.firstChild.nodeValue = "用户名可以使用";
document.getElementById("mySubmit").disabled = false;
}
}
}
}

//用于校验用户名的函数
function checkUserName()
{
//创建XMLHttpRequest对象
creatXmlHTTP();
if (xmlHttp!=null)
{
//定义用于响应XMLHttpRequest对象状态变化的函数
xmlHttp.onreadystatechange = checkData;
//创建HTTP请求
xmlHttp.open("get","userName.txt",true);
//发送HTTP请求
xmlHttp.send(null);
}
else
{
alert("您的浏览器不支持XMLHTTP,请更换浏览器。");
}
}
-->
</script>
</head>
<body>
<p align="center"><b>用户注册</b></p>
<form name="myForm">
用户名:<input type="text" name="username" onblur="checkUserName()">
<span id="userInfo">&nbsp;</span><br>
性别:<input type="text"><br>
邮编:<input type="text"><br>
地址:<input type="text"><br>
<input type="button" value="提交" id="mySubmit" disabled="true">
</form>
</body>
</html>
txt内容:
张三、李四、王五、钱六
在IE输入以上内容,有反应,而谷歌浏览器没有正常,求助

解决方案 »

  1.   

    function creatXmlHTTP()
    {
    //if(window.ActiveXObject)
    if(isIe)
    {
    var arrActiveX = ['Microsoft.XMLHTTP','MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP'];
    for(var i=0;i<arrActiveX.length;i++)
    {
    try
    {
    xmlHttp = new ActiveXObject(arrActiveX[i]);
    break;
    }
    catch(ex)
    {
    }
    }
    }//else if(window.XMLHttpRequest){
    else{
    xmlHttp = new XMLHttpRequest();
    }
    }
    改成
    function creatXmlHTTP()
    {
    xmlHttp = window.XMLHttpRequest? new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
    }就可以了另外,
    xmlHttp.open("get","userName.txt?tmp=" + (new Date()).valueOf(),true);防止缓存
      

  2.   

    myForm.username.value
    最好写成
    document.myForm.username.value规范的写法
      

  3.   

    另外,
    node.firstChild.nodeValue = "用户名已经存在,请重新输入";这种写法也不规范
      

  4.   

    I would highly recommend not using XMLHttpRequest directly due to the difference on IE and non IE. it is very difficult handling these. so use JQuery or other javascript library. Here are some of the reasons why you should use JQueryhttp://www.java101.net/thread/listthreads.xhtml?ctgtThdId=14
      

  5.   

    最好写一个比较通用的xmlhttprequest请求
      

  6.   


    function getCreatethttpRequest() {
    var httpRequest = false;
    if (window.XMLHttpRequest) {
    // ����Mozilla�pNetscape�pSafari�������������XMLHttpRequest
    httpRequest = new XMLHttpRequest();
    if (httpRequest.overrideMimeType) {
    // ����������Ӧ��header����text/xml�����Ե��������޸ĸ�header
    httpRequest.overrideMimeType('text/xml');
    }
    } else if (window.ActiveXObject) {
    // ����Internet Explorer�����������XMLHttpRequest
    try {
    httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
    }
    }
    }
    return httpRequest;
    function send_with_ajax() {
    var httpRequest = getCreatethttpRequest();
    httpRequest.onreadystatechange = function() {
    alertContents(httpRequest);
    };
    httpRequest.open("GET",
    "http://127.0.0.1:8080/javaJqury/servlet/Ajax_Jquery", true);
    httpRequest.send(null);
    }function alertContents(httpRequest) {
    if (httpRequest.readyState == 4) {
    if ((httpRequest.status == 200) || (httpRequest.status == 0)) {
    var htmlDoc = document.createElement('div');
    htmlDoc.innerHTML = httpRequest.responseText;
    var jsonString = httpRequest.responseText;
    var jsonObjs = eval('(' + jsonString + ')');
    var continents = jsonObjs.jobs;
    for (var i = 0; i < continents.length; i++) {
    var newLine = $("#planTable").length;
    var row = planTable.insertRow(newLine);
    var col = row.insertCell(0);
    col.innerHTML = continents[i].name;
    col = row.insertCell(1);
    col.innerHTML = continents[i].age;
    col = row.insertCell(2);
    col.innerHTML = continents[i].email;
    col = row.insertCell(3);
    col.innerHTML = "send_with_ajax";
    }
    } else {
    alert('There was a problem with the request. ' + httpRequest.status
    + httpRequest.responseText);
    }
    }
      

  7.   

    function createRequest(){
        try{
         request=new XMLHttpRequest();   
        }catch(tryMS){
           try{
           request=new ActiveXObject("Msxml2.XMLHTTP");    
           } catch(otherMS){
               try{
            request=new ActiveXObject("Microsoft.XMLHTTP");       
               }catch(failed){
           request=null;        
               }
           }
        }
      return request;  
    }通用的request