2019-08-22 AJAX第二天

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

post文件上传

<?php
print_r($_POST);

echo"<br>";

print_r($_FILES);

echo"<br>";

//1.获取上传文件的对应的字典

$fileInfo = $_FILES["upfile"];
echo"<br>";
print_r($fileInfo);

//2.获取上传文件的名字

$fileName = $fileInfo["name"];
echo"<br>";

//3.获取上传文件的路径
$filePath = $fileInfo["tmp_name"];
echo"<br>";

//4.打印名字和路径


echo $fileName;
echo"<br>";
echo $filePath;
echo"<br>";

//5.移动全新目录  上传失败的把destination:去掉就可以了

move_uploaded_file($filePath,"./source/".$fileName);


 ?> 

post大文件上传

修改上传设置

图片.png

GET基本使用

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button>发送请求</button>
    <script>
        window.onload = function(ev) {
            var num = Math.floor(Math.random() * 10)
            var data = new Date().getTime()
            console.log(num, data)
            var oBtn = document.querySelector('button');
            oBtn.onclick = function(ev1) {
                //1创建一个异步对象
                var xhr;
                if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
                    xhr = new XMLHttpRequest();
                } else { // code for IE6, IE5
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                //所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject)。

                //var xhr = new XMLHttpRequest();

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

                xhr.open("GET", "05-ajax-get.txt?tn=" + "data", true);
                //第三个参数永远true

                //3发送请求

                xhr.send();

                //4监听状态变化

                xhr.onreadystatechange = function(ev2) {

                    if (xhr.readyState === 4) {
                        //5处理返回的结果
                        if (xhr.status >= 200 & xhr.status <= 300 | xhr.status === 304) {
                            alert("success")
                                //document.write(xhr.responseText)
                        } else {
                            alert("false")
                        }
                    }
                    /*在ie浏览器中如果通过ajax发送请求  那么ie浏览器人为同一个URL
                    只有一个结果*/

                    /*
                    

                        存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。

                            0: 请求未初始化
                            1: 服务器连接已建立
                            2: 请求已接收
                            3: 请求处理中
                            4: 请求已完成,且响应已就绪

                        */
                }
            }
        }
    </script>
</body>

</html>

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调用部分

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="myAjax.js"></script>
</head>

<body>
    <button>发送请求</button>
    <script>
        window.onload = function(ev) {
            var res = encodeURIComponent("wd=张三");
            var oBtn = document.querySelector('button');
            console.log(res)
            oBtn.onclick = function(ev1) {

                ajax("文字.php", function(xhr) {
                        alert(xhr.responseText);
                    }, 3000 //设置超时时间
                    ,
                    function(xhr) {
                        alert("请求失败")
                    })
            }
        }
    </script>
</body>

</html>

jQuery封装

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../jquery-3.4.1.js"></script>

</head>

<body>
    <button>发送请求</button>
    <script>
        $(function(ev) {
            $("button").click(function(ev1) {
                    $.ajax({
                        type: "GET",
                        url: "09-ajax.php",
                        data: "userName=lkf&userPwd=654321",
                        success: function(msg) {
                            document.write(msg);
                        },
                        error: function(xhr) {
                            alert(xhr.status);
                        }
                    });
                }

            )
        })
    </script>
</body>

</html>
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封装最终版

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!--<script src="../jquery-3.4.1.js"></script>-->
    <script src="ajax封装.js"></script>

    <script>
        window.onload = function(ev) {
                var oBtn = document.querySelector("button");
                oBtn.onclick = function(ev1) {
                    /*   ajax("POST",
                        "ajax封装.php", {
                            "userName": "三生石",
                            "userPwd": "123456"
                                //服务器需要的格式:url?key=value&key = value;
                        }, 3000,
                        function(xhr) {
                            alert(xhr.responseText);
                        },
                        function(xhr) {
                            alert("请求失败");
                        })
                };*/
                    ajax({
                        success: function(xhr) {
                            alert(xhr.responseText)
                        },
                        url: 'ajax封装.php',
                        data: {
                            "userName": "三生石",
                            "userPwd": "123456"
                        },
                        type: "post",
                        timeout: "3000",

                        error: function(xhr) {
                            alert(xhr.status)
                        }
                    })
                }
            }
            /* $(function() {
                 $("button").click(function() {
                     $.ajax({
                         type: "GET",
                         url: "ajax封装.php",
                         data: "userName=三生石&userPwd=123456 ",
                         success: function(msg) {
                             alert(msg);
                         },
                         error: function(xhr) {
                             alert(xhr.status)
                         }
                     });
                 })
             })*/
    </script>
</head>

<body>

    <button>发送请求</button>

</body>

</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260

推荐阅读更多精彩内容

  • 一、 认识Ajax 1、 初识 ajax 我们平常上网,不管是注册账号,还是浏览网页,其本质就是通过客户端向服务器...
    宠辱不惊丶岁月静好阅读 966评论 0 2
  • Ajax的基本概念及使用 同步&异步 同步:必须等待前面的任务完成,才能继续后面的任务; 异步:不受当前主要任务的...
    magic_pill阅读 1,887评论 0 5
  • 一:什么是闭包?闭包的用处? (1)闭包就是能够读取其他函数内部变量的函数。在本质上,闭包就 是将函数内部和函数外...
    xuguibin阅读 9,192评论 1 52
  • 开始很容易,但是坚持很难! 最近一直和朋友们探讨创业这条路!朋友们都说很佩服我做事的毅力,做每一件...
    果子的妈妈美芳阅读 302评论 0 1
  • 霍金去世了。刚刚得知这个消息时候我反复刷着微博,直到看到了BBC的通告,终于相信了这个事实。在我的印象里,霍金就像...
    草垛阅读 1,596评论 0 2