• 原生ajax


    function ajax(url, fnSucc, fnFaild)
    {
        //1.创建
        if(window.XMLHttpRequest)
        {
            var oAjax=new XMLHttpRequest();
        }
        else
        {
            var oAjax=new ActiveXObject('Microsoft.XMLHTTP');
        }
        
        //2.连接
        oAjax.open('GET', url, true);
        
        //3.发送
        oAjax.send();
        
        //4.接收
        oAjax.onreadystatechange=function ()
        {
            if(oAjax.readyState==4)        //完成
            {
                if(oAjax.status>=200 && oAjax.status<300 || oAjax.status==304)
                {
                    //alert('成功:'+oAjax.responseText);
                    if(fnSucc)
                    {
                        fnSucc(oAjax.responseText);
                    }
                }
                else
                {
                    //alert('失败:'+oAjax.status);
                    if(fnFaild)
                    {
                        fnFaild(oAjax.status);
                    }
                }
            }
        };
    }

    辅助函数处理url

    function json2url(json){
        json.t=Math.random();
    
        var arr=[];
    
        for(var i in json)
        {
            arr.push(i+'='+json[i]);
        }
    
        return arr.join('&');
    }

    实例1:读取文字

    复制代码
    window.onload=function ()
    {
        var oBtn=document.getElementById('btn1');
        
        oBtn.onclick=function ()
        {
            //alert('aaa.txt?t='+Math.random());
            ajax('aaa.txt?t='+Math.random(), function (str){
                //str——从服务器读回来的内容
                alert(str);
            });
        };
    };
    复制代码

    实例2:注册和登陆

    复制代码
    window.onload=function(){
        var oAddUser=document.getElementById('add_user');
        var oAddPass=document.getElementById('add_pass');
        var oAddBtn=document.getElementById('add_btn');
    
        oAddBtn.onclick=function()
        {
            var url='user.php?'+json2url({
                act:'add',
                user:oAddUser.value,
                pass:oAddPass.value
            })
    
            ajax(url,function(str){
                var json=eval('('+str+')');
                if(json.error){
                    alert('有问题'+json.desc);
                }else{
                    alert('注册成功');
                }
            },function(){
                alert('失败');
            });
        };
    
        var oLgnUser=document.getElementById('login_user');
        var oLgnPass=document.getElementById('login_pass');
        var oLgnBtn=document.getElementById('login_btn');
    
        oLgnBtn.onclick=function(){
            var url='user.php?'+json2url({
                act:'login',
                user:oLgnUser.value,
                pass:oLgnPass.value
            })
    
            ajax(url,function(str){
                var json=eval('('+str+')');
                if(json.error){
                    alert('有问题'+json.desc);
                }else{
                    alert('登陆成功');
                }
            },function(){
                alert('失败');
            })
        };
    };
    复制代码
  • 相关阅读:
    2、MySQL语法规范 与 注释
    5、手写代码实现MyBatis的查询功能
    1、MySQL常见的操作命令
    操作系统(五)——文件
    操作系统(四)——内存
    操作系统(三)——信号量、死锁
    操作系统(二)——进程与线程
    操作系统(一)——概述和进程与线程基础
    多线程与并发(四)——线程池、原子性
    开课博客
  • 原文地址:https://www.cnblogs.com/mingjixiaohui/p/5246370.html
Copyright © 2020-2023  润新知