2019-08-22 AJAX第二天

之前的算打基础——好戏才刚刚开始

post文件上传

";

print_r($_FILES);

echo"
"; //1.获取上传文件的对应的字典 $fileInfo = $_FILES["upfile"]; echo"
"; print_r($fileInfo); //2.获取上传文件的名字 $fileName = $fileInfo["name"]; echo"
"; //3.获取上传文件的路径 $filePath = $fileInfo["tmp_name"]; echo"
"; //4.打印名字和路径 echo $fileName; echo"
"; echo $filePath; echo"
"; //5.移动全新目录 上传失败的把destination:去掉就可以了 move_uploaded_file($filePath,"./source/".$fileName); ?>

post大文件上传

修改上传设置

2019-08-22 AJAX第二天_第1张图片
图片.png

GET基本使用





    
    
    
    Document



    
    




ie兼容


GET/POST封装

ajax封装部分

function ajax(url, success, timeout, error) {
    //1创建一个异步对象
    var xmlhttp, timer;
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    };
    //所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject)。

    //var xhr = new XMLHttpRequest();

    //2设置请求方式和请求地址

    xmlhttp.open(method, encodeURIComponent(url) + "?tn=" + (new Date().getTime()), true); //第三个参数永远true

    //3发送请求

    xmlhttp.send();

    //4监听状态变化

    xmlhttp.onreadystatechange = function(ev2) {

        if (xmlhttp.readyState === 4) {

            clearInterval(timer);

            //5处理返回的结果
            if (xmlhttp.status >= 200 & xmlhttp.status <= 300 | xmlhttp.status === 304) {
                success(xmlhttp);
            } else {
                error(xmlhttp);
            }
        };
    };
    //判断外界是否传入了超时时间
    if (timeout) {
        timer = setInterval(function() {
            //中断请求
            console.log("中断请求");
            xmlhttp.abort();
            clearInterval(timer);
        }, timeout)
    }

}

html调用部分





    
    
    
    Document
    



    
    




jQuery封装





    
    
    
    Document
    




    
    



function ajax(method, url, success, timeout, error) {
    //1创建一个异步对象
    var xmlhttp, timer;
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    };
    //所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject)。

    //var xhr = new XMLHttpRequest();

    option.method.touppercase()
    console.log(option.method)

    option.url += "?tn=" + new Data().gettime()


    //2设置请求方式和请求地址

    xmlhttp.open(option.obj); //第三个参数永远true



    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    //3发送请求

    xmlhttp.send();

    //4监听状态变化

    xmlhttp.onreadystatechange = function(ev2) {

        if (xmlhttp.readyState === 4) {

            clearInterval(timer);

            //5处理返回的结果
            if (xmlhttp.status >= 200 & xmlhttp.status <= 300 | xmlhttp.status === 304) {
                option.success(xmlhttp);
            } else {
                option.error(xmlhttp);
            }
        };
    };
    //判断外界是否传入了超时时间
    if (option.timeout) {
        timer = setInterval(function() {
            //中断请求
            console.log("中断请求");
            xmlhttp.abort();
            clearInterval(timer);
        }, timeout)
    }

}

XML

可扩展的标记语言

完犊子哦


ajax 原生js封装最终版





    
    
    
    Document
    
    

    




    




你可能感兴趣的:(2019-08-22 AJAX第二天)