zoukankan      html  css  js  c++  java
  • ajax核心代码(转载)

    以下代码是经过本人大量实验,结合他人资料总结出来一段ajax核心代码,比较灵活,传参可以用POST和GET,可以返回TEXT和XML
    <script language="javascript">
    // JavaScript Document
    var http_request = false;
    //发送请求,第一个参数为目的地址,第二个参数为请求方法只有"GET"、"POST"
    //第三个参数为需要传送的参数列表key1=value1&key2=value2
    //第四个参数为响应信息类型只能为"TEXT"或"XML"
    function send_request(url,method,params,responseType)
    {//初始化、指定处理函数、发送请求的函数
    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 = function(){processRequest(responseType)};
    if (method=="GET")
    {
    // 确定发送请求的方式和URL以及是否同步执行下段代码
    http_request.open(method, url+"?"+params, true);
    http_request.send(null);
    }
    else if(method=="POST")
    {
    http_request.open(method, url, true);
    http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    http_request.send(params);
    }
    else 
    {
    alert("无效的method!method只能为:GET或POST");
    }
    }

    // 处理返回信息的函数
    function processRequest(responseType)
    {
            if (http_request.readyState == 4)
    { // 判断对象状态
                if (http_request.status == 200) 
    { // 信息已经成功返回,开始处理信息

    if (responseType=="TEXT")
    {
                    alert(http_request.responseText);
    }
    else if(responseType=="XML")
    {
    alert(http_request.responseXML);
    }
    else
    {
    alert("无效的响应信息类型!只能为:TEXT或XML");
    }

    /*
    alert(http_request.responseText);
    */

                } 
    else 
    { //页面不正常
                    alert("您所请求的页面有异常。");
                }
            }
    }

    function userCheck() 
    {
    alert("a");
    send_request('postback.asp',"POST","name=arlang","TEXT");
    alert("b");
    }
    </script>


    ---------------------------------------------------------------------------------------------------------------------------------
    copyright:http://www.cnblogs.com/anee/
  • 相关阅读:
    Dependency Injection in ASP.NET Web API 2
    js, lambada? 在chrome和node下可以使用
    jquery text
    bugs view:
    支持 gRPC 长链接,深度解读 Nacos 2.0 架构设计及新模型
    阿里云 ecs云主机 静默安装oracle11g
    mysql1033错误 InnoDB临时表空间报错
    8888. Distance Between 2 Nodes in BST
    783. Minimum Distance Between BST Nodes
    530. Minimum Absolute Difference in BST
  • 原文地址:https://www.cnblogs.com/anee/p/2675940.html
Copyright © 2011-2022 走看看