首页 > 编程 > .NET > 正文

ASP.NET技巧:创建不了XMLHTTP控件

2024-07-10 13:09:16
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 最近在用ajax开发服务器程序,发现ie浏览器不支持xmlhttprequest对象,而且找不到microsoft.xmlhttp控件。

    问题出现了我们需要解决,解决方案如下:

    1、运行下regsvr32 msxml3.dll;
    2、用现成的框架来做ajax;
    3、代码优化:

    if(window.activexobject)
        {
            xmlhttp = new activexobject("microsoft.xmlhttp");
        }
        else if(window.xmlhttprequest)
        {
            xmlhttp = new xmlhttprequest();
        }

        if(handle_s == null)
            handle_s = "bin/normal.py/db";
        this.xmlhttp.onreadystatechange = handle_l;
        this.xmlhttp.open("get",handle_s,true);
        this.xmlhttp.send(null);

    或判断浏览器

    var agt = navigator.useragent.tolowercase();
    var is_ie = (agt.indexof("msie") != -1);
    var is_ie5 = (agt.indexof("msie 5") != -1);
    var is_opera = (agt.indexof("opera") != -1);
    var is_mac = (agt.indexof("mac") != -1);
    var is_gecko = (agt.indexof("gecko") != -1);
    var is_safari = (agt.indexof("safari") != -1);

    function createxmlhttpreq(handler) {

    var xmlhttp = null;
    if (is_ie) {
    // guaranteed to be ie5 or ie6
    var control = (is_ie5) ? "microsoft.xmlhttp" : "msxml2.xmlhttp";

    try {
    xmlhttp = new activexobject(control);
    xmlhttp.onreadystatechange = handler;
    } catch (ex) {
    // todo: better help message
    alert("you need to enable active scripting and activex controls");
    }

    } else {

    // mozilla
    xmlhttp = new xmlhttprequest();
    xmlhttp.onload = handler;
    xmlhttp.onerror = handler;

    }

    return xmlhttp;
    }

    或者

    <script language="javascript">
        var http_request = false;
        function send_request(url) {//初始化、指定处理函数、发送请求的函数
            http_request = false;
            //开始初始化xmlhttprequest对象
            if(window.xmlhttprequest) { //mozilla 浏览器
                http_request = new xmlhttprequest();
                if (http_request.overridemimetype) {//设置mime类别
                    http_request.overridemimetype('text/xml');
                }
            }
            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) { // 异常,创建对象实例失败
                window.alert("不能创建xmlhttprequest对象实例.");
                return false;
            }
            http_request.onreadystatechange = processrequest;
            // 确定发送请求的方式和url以及是否同步执行下段代码
            http_request.open("get", url, true);
            http_request.send(null);
        }
        // 处理返回信息的函数
        function processrequest() {
            if (http_request.readystate == 4) { // 判断对象状态
                if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
                    var returnobj = http_request.responsexml;
                    var xmlobj = http_request.responsexml;
                    var employees = xmlobj.getelementsbytagname("employee");
                    var feedbackstr = "";
                    for(var i=0;i<employees.length;i++) { // 循环读取employees.xml的内容
                        var employee = employees[i];
                        feedbackstr += "员工:" + employee.getattribute("name");//取得标签指定属性
                        feedbackstr += " 职位:" + employee.getelementsbytagname("job")[0].firstchild.data;//取得指定标签的最初数据
                        feedbackstr += " 工资:" + employee.getelementsbytagname("salary")[0].firstchild.data;
                        feedbackstr +=  "/r/n";
                    }
                    alert(feedbackstr);
                } else { //页面不正常
                    alert("您所请求的页面有异常。");
                }
            }
        }
    </script>

    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表