• ajax基础实例 返回xml


    //AJAX request for the city data
    function ajaxforget(cn) {
        var xmlhttp_request = "";
        try {
            if (window.ActiveXObject) {
                for (var i = 5; i; i--) {
                    try {
                        if (i == 2) {
                            xmlhttp_request = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        else {
                            xmlhttp_request = new ActiveXObject("Msxml2.XMLHTTP." + i + ".0");
                        }
                        break;
                    }
                    catch (e) {
                        xmlhttp_request = false;
                    }
                }
            }
            else if (window.XMLHttpRequest) {
                xmlhttp_request = new XMLHttpRequest();
            }
        }
        catch (e) { xmlhttp_request = false; }
        if (!xmlhttp_request) {
            alert("您的浏览器不支持ajax");
            return;
        }
        xmlhttp_request.open('GET', '../Handler/CityData.ashx?code='+cn+'&time='+new Date().getMilliseconds(), true);

        xmlhttp_request.setRequestHeader("Content-Type", "text/xml"); //setRequestHeader("header", "value")
        xmlhttp_request.setRequestHeader("Charset", "gb2312"); //把指定首部设置为所提供的值。在设置任何首部之前必须先调用open()
        xmlhttp_request.send(null);
        var self = xmlhttp_request; //把引用赋给临时变量self
        xmlhttp_request.onreadystatechange = function() {
            thecallback(self);
        };
    }

            //ajax的回调函数
            function thecallback(xmlhttp_request) {
                if (xmlhttp_request.readyState == 4 && xmlhttp_request.status == 200) {
                    //绑定项
                    var data = xmlhttp_request.responseXML;
                    var citys = data.getElementsByTagName("name");
                    var cns = data.getElementsByTagName("code");
                    var listCity = $("listCity");
                    listCity.innerHTML = ""; //先移除所有的项
                    var listP = $("listProvince");
                    var code = listP.options[listP.selectedIndex].value;
                    var item = new Array();
                    for (var i = 0; i < citys.length; i++) {
                        if (citys.length != 1 && code == cns[i].childNodes[0].data) continue;
                        item[i] = document.createElement("option");
                        item[i].value = cns[i].childNodes[0].data;
                        item[i].innerHTML = citys[i].childNodes[0].data;
                        listCity.appendChild(item[i]);
                    }
                    //绑定项结束
                    $("txtCode").value = listCity.options[0].value;
                }
            }

  • 相关阅读:
    Python selenium —— 父子、兄弟、相邻节点定位方式详解
    Python爬虫入门教程石家庄链家租房数据抓取
    自动化测试 selenium 环境搭建
    python3爬虫(find_all用法等)
    python网络爬虫抓取动态网页并将数据存入数据库MySQL
    mvc设计模式和mvc框架的区别
    python正则表达式贪婪与非贪婪模式
    python3获取一个网页特定内容
    使用exceljs 前端导出表格并自定义样式和超链接
    如何在devtools中找到未使用 JavaScript 和 CSS 代码
  • 原文地址:https://www.cnblogs.com/Denny_Yang/p/1962454.html
Copyright © 2020-2023  润新知