首页 > Web开发 > 详细

获得xmlhttp对象

时间:2020-06-08 22:30:28      阅读:48      评论:0      收藏:0      [点我收藏+]
//获取用户输入内容的关联信息的函数
function getMoreContents(){
    var xmlHttp;
    //首先要获得用户的输入
    var content=document.getElementById("keyword");
    if(content.value==""){
        return;
    }
    //然后给服务器发送对象输入的内容,ajax异步发送数据
    //使用一个对象,叫做XmlHttp对象
    xmlHttp=creatXmlHttp();
    //给服务器发送数据
    var url="search?keyword="+escape(content.value);
    //true表示js脚本在send()方法后继续执行
    xmlHttp.open("GET",url,true);
    //xmlHttp绑定回调方法,该方法会在xmlHttp状态改变时被调用
    //xmlHttp的状态0-4,只关心4(complete)这个状态
    xmlHttp.onreadystatechange=callback;
    xmlHttp.send(null);
}
//获得xmlhttp对象
function creatXmlHttp(){
    //对大多数浏览器适用
    var xmlHttp;
    if(window.XMLHttpRequest){
        xmlHttp=new XMLHttpRequest();
    }
    //考虑浏览器兼容性
    if(window.ActiveXObject){
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        if(!xmlHttp){
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
    }
    return xmlHttp;
}
function callback(){
    if(xmlHttp.readyState==4){
        //200代表响应成功,404未找到,500内部错误
        if(xmlHttp.status==200){
            //交互成功,获得相应数据,文本格式
            var result=xmlHttp.responseText;
            //解析获得数据
            var json=eval("("+result+")");
            //获得数据,动态显示数据
            
        }
    }
}

 

//获取用户输入内容的关联信息的函数function getMoreContents(){var xmlHttp;//首先要获得用户的输入var content=document.getElementById("keyword");if(content.value==""){return;}//然后给服务器发送对象输入的内容,ajax异步发送数据//使用一个对象,叫做XmlHttp对象xmlHttp=creatXmlHttp();//给服务器发送数据var url="search?keyword="+escape(content.value);//true表示js脚本在send()方法后继续执行xmlHttp.open("GET",url,true);//xmlHttp绑定回调方法,该方法会在xmlHttp状态改变时被调用//xmlHttp的状态0-4,只关心4(complete)这个状态xmlHttp.onreadystatechange=callback;xmlHttp.send(null);}//获得xmlhttp对象function creatXmlHttp(){//对大多数浏览器适用var xmlHttp;if(window.XMLHttpRequest){xmlHttp=new XMLHttpRequest();}//考虑浏览器兼容性if(window.ActiveXObject){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");if(!xmlHttp){xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}}return xmlHttp;}function callback(){if(xmlHttp.readyState==4){//200代表响应成功,404未找到,500内部错误if(xmlHttp.status==200){//交互成功,获得相应数据,文本格式var result=xmlHttp.responseText;//解析获得数据var json=eval("("+result+")");//获得数据,动态显示数据}}}

获得xmlhttp对象

原文:https://www.cnblogs.com/nirvanaInSilence/p/13068721.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!