zoukankan      html  css  js  c++  java
  • ajax

    一、js

    function ajaxGet(url,fnSucc,fnFaild){
        //1.创建Ajax对象
        //用没有定义的变量---报错
        //用没有定义的属性---undefined
        if (window.XMLHttpRequest) {
            //非IE6
            var oAjax=new XMLHttpRequest();
        } 
        else{
            //IE6
            var oAjax=new ActiveXObject("Microsoft.XMLHTTP");
        };
    
        //2.链接服务器
        //open(方法,文件名,异步传输)
        oAjax.open("GET",url+"?time="+new Date().getTime(),true);
    
        //3.发送请求
        oAjax.send();
    
        //4.接收返回
        oAjax.onreadystatechange=function(){
            //oAjax.readyState    //浏览器和服务器,进行到哪一步了
    
            if (oAjax.readyState==4) {    //读取完成
                if (oAjax.status==200) {    //成功
                    fnSucc(oAjax.responseText);
                }
                else{
                    if (fnFaild) {
                        fnFaild(oAjax.status);
                    };
                };
            };
        };
    };
    
    function ajaxPost(url,json,fnSucc,fnFaild){
        //1.创建Ajax对象
        //用没有定义的变量---报错
        //用没有定义的属性---undefined
        if (window.XMLHttpRequest) {
            //非IE6
            var oAjax=new XMLHttpRequest();
        } 
        else{
            //IE6
            var oAjax=new ActiveXObject("Microsoft.XMLHTTP");
        };
    
        //2.链接服务器
        //open(方法,文件名,异步传输)
        oAjax.open("POST",url,true);
    
        oAjax.setRequestHeader("Content-Type","application/json");
    
        //3.发送请求
        oAjax.send(JSON.stringify(json));
    
        //4.接收返回
        oAjax.onreadystatechange=function(){
            //oAjax.readyState    //浏览器和服务器,进行到哪一步了
    
            if (oAjax.readyState==4) {    //读取完成
                if (oAjax.status==200) {    //成功
                    fnSucc(oAjax.responseText);
                }
                else{
                    if (fnFaild) {
                        fnFaild(oAjax.status);
                    };
                };
            };
        };
    };

    二、jquery

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>ajax</title>
        <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>
    <script>
        $.ajax({
            url: "",//请求地址
            type: "",//请求方式(post、get)
            data: "",//请求参数
            success: function () {
            },//成功回调
            error: function () {
            },//失败回调
            dataType: ""//响应数据的格式(text、json),默认会根据MIME信息来智能判断
        })
        // $.post(url,[data],[callback],[type])
    </script>
    </body>
    </html>
  • 相关阅读:
    Clipper库中文文档详解
    JavaScript-Clipper.js
    安装Scrapy遇到的问题
    Python中if __name__ == '__main__'的使用
    写出一段Python代码实现删除一个list里面的重复元素
    Python 内置函数(反射类)
    Python 内置函数(集合操作,IO操作)
    Python 内置函数(数学运算类,逻辑判断类)
    Python 推导式(列表推导式,字典推导式,集合推导式)
    Python 闭包和装饰器
  • 原文地址:https://www.cnblogs.com/linding/p/13605661.html
Copyright © 2011-2022 走看看