国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

原生js創(chuàng)建get/post請求以及封裝方式、axios的基本使用

這篇具有很好參考價(jià)值的文章主要介紹了原生js創(chuàng)建get/post請求以及封裝方式、axios的基本使用。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

原生js創(chuàng)建get請求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="box"></div>
    <script>
        //創(chuàng)建ajax引擎對象
        let xhr = new XMLHttpRequest();

        //配置請求方式和請求地址
        xhr.open("get","http://ajax.h5.itsource.cn:5000/api/testGet?name=張三");

        // 監(jiān)聽響應(yīng)碼和狀態(tài)碼
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4 && xhr.status == 200){
                // 處理數(shù)據(jù)
                let res = JSON.parse(xhr.responseText);
                console.log(res);

                //判定再渲染
                if(res.code == 200){
                    box.innerHTML = res.data; 
                }
            }
        }
        // 發(fā)送請求
        xhr.send();
    </script>
</body>
</html>

原生js創(chuàng)建post請求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="box"></div>
    <script>
        //創(chuàng)建ajax引擎對象
        let xhr = new XMLHttpRequest();

        //配置請求方式和請求地址
        xhr.open("post","http://ajax.h5.itsource.cn:5000/api/testPost");

        // 監(jiān)聽狀態(tài)變化和接收數(shù)據(jù)
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4 && xhr.status == 200){
                // 處理數(shù)據(jù)
                let res = JSON.parse(xhr.responseText);
                console.log(res);

                //判定再渲染
                if(res.code == 200){
                    box.innerHTML = res.data; 
                }
            }
        }

        //請求頭
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        // 發(fā)送請求
        xhr.send("name=李四");
    </script>
</body>
</html>

原生get和post封裝方式1

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

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

<body>
    <script>
        function ajax(type, url, parmas, callback) {

            //創(chuàng)建ajax引擎對象
            let xhr = new XMLHttpRequest();
            //處理參數(shù),定義一個(gè)空數(shù)組
            let arr = [];
            //遍歷對象,拼接到數(shù)組中
            for (const key in parmas) {
                if (Object.hasOwnProperty.call(parmas, key)) {
                    arr.push(key + "=" + parmas[key]);                   
                }
            }
            parmas = arr.join("&");

            if (type == "get") {
                //配置請求方式和請求地址
                xhr.open(type, url + "?"+ parmas);
                // 發(fā)送請求
                xhr.send();
            } else {
                //配置請求方式和請求地址
                xhr.open(type, url);
                //請求頭
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                // 發(fā)送請求
                xhr.send(parmas);
            }

            // 監(jiān)聽狀態(tài)變化和接收數(shù)據(jù)
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    // 處理數(shù)據(jù)
                    callback(JSON.parse(xhr.responseText));
                }
            }

        }

        ajax("get","http://ajax.h5.itsource.cn:5000/api/testGet",{
            name:"張三"
        },function(res){
            console.log(res);
        })

        ajax("post","http://ajax.h5.itsource.cn:5000/api/testPost",{
            name:"李四"
        },function(res){
            console.log(res);
        })
    </script>
</body>

</html>

原生get和post封裝方式2

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

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

<body>
    <script>

        // //方式二:
        function ajax(obj) {
            //處理參數(shù)
            let type = obj.type;
            let url  = obj.url;
            let parmas = obj.params;
            let callback = obj.callback;

            //創(chuàng)建ajax引擎對象
            let xhr = new XMLHttpRequest();
            //處理參數(shù),定義一個(gè)空數(shù)組
            let arr = [];
            //遍歷對象,拼接到數(shù)組中
            for (const key in parmas) {
                if (Object.hasOwnProperty.call(parmas, key)) {
                    arr.push(key + "=" + parmas[key]);
                }
            }
            parmas = arr.join("&");

            // 判定
            if (type == "get") {
                //配置請求方式和請求地址
                xhr.open(type, url + "?" + parmas);
                // 發(fā)送請求
                xhr.send();
            } else {
                //配置請求方式和請求地址
                xhr.open(type, url);
                //請求頭
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                // 發(fā)送請求
                xhr.send(parmas);
            }

            // 監(jiān)聽狀態(tài)變化和接收數(shù)據(jù)
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    // 處理數(shù)據(jù)
                    callback(JSON.parse(xhr.responseText));

                }
            }

        }

        // 調(diào)用
        ajax({
            type: "get",
            url: 'http://ajax.h5.itsource.cn:5000/api/testGet',
            params: {
                name: '張三'
            },
            callback: function (res) {
                console.log(res)
            }
        })

        ajax({
            type: "post",
            url: 'http://ajax.h5.itsource.cn:5000/api/testPost',
            params: {
                name: '李四'
            },
            callback: function (res) {
                console.log(res)
            }
        })


    </script>
</body>

</html>

axios的基本使用文章來源地址http://www.zghlxwxcb.cn/news/detail-832214.html

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

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

<body>
    <script>
        // 為給定 ID 的 user 創(chuàng)建請求
        axios.get('http://ajax.h5.itsource.cn:5000/api/testGet?name=張三')
            .then(function (response) {
                console.log(response.data);
            })
            .catch(function (error) {
                console.log(error);
            });

        // 上面的請求也可以這樣做
        axios.get('http://ajax.h5.itsource.cn:5000/api/testGet', {
            params: {
                ID: 12345
            } 
        })
            .then(function (response) {
                console.log(response.data);
            })
            .catch(function (error) {
                console.log(error);
            });
    </script>
</body>

</html>

到了這里,關(guān)于原生js創(chuàng)建get/post請求以及封裝方式、axios的基本使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • ajax-axios發(fā)送 get請求 或者 發(fā)送post請求帶有請求體參數(shù)
  • java封裝https的get、post請求

    話不多說,直接進(jìn)入正題。 原生的方法,java8中全部都有

    2024年02月11日
    瀏覽(25)
  • ElementUI之登陸+注冊->餓了嗎完成用戶登錄界面搭建,axios之get請求,axios之post請求,跨域,注冊界面

    ElementUI之登陸+注冊->餓了嗎完成用戶登錄界面搭建,axios之get請求,axios之post請求,跨域,注冊界面

    ?餓了嗎完成用戶注冊登錄界面搭建 axios之get請求 axios之post請求 跨域 注冊界面 1.餓了嗎完成用戶注冊登錄界面搭建 將端口號8080改為8081 導(dǎo)入依賴,在項(xiàng)目根目錄使用命令npm install element-ui -S,添加Element-UI模塊 -g:將依賴下載node_glodal全局依賴 -d(依賴放在static/[]package.json的

    2024年02月04日
    瀏覽(29)
  • 再vue項(xiàng)目中使用axios原生發(fā)送post請求

    前言:在大多數(shù)項(xiàng)目開發(fā)中,都是采用前后端分離架構(gòu),在此情況下都采用一些成熟的框架,類似于ruoyi,因?yàn)槌墒焖郧岸说恼埱蠖歼M(jìn)行了各種封裝,有時(shí)想單獨(dú)發(fā)起一個(gè)簡單的請求,還有點(diǎn)麻煩,因此記錄一下。 因?yàn)槭乔昂蠖朔蛛x,當(dāng)前前端使用的端口是81,后端運(yùn)行的

    2024年02月14日
    瀏覽(29)
  • 【axios網(wǎng)絡(luò)請求庫】認(rèn)識Axios庫;axios發(fā)送請求、創(chuàng)建實(shí)例、創(chuàng)建攔截器、封裝請求

    功能特點(diǎn): 在瀏覽器中發(fā)送 XMLHttpRequests 請求 在 node.js 中發(fā)送 http請求 支持 Promise API 攔截請求和響應(yīng) 轉(zhuǎn)換請求和響應(yīng)數(shù)據(jù) 支持多種請求方式: axios(config) axios.request(config) axios.get(url[, config]) axios.delete(url[, config]) axios.head(url[, config]) axios.post(url[, data[, config]]) axios.put(url[, data[, c

    2024年02月10日
    瀏覽(65)
  • axios的post請求所有傳參方式

    Axios支持多種方式來傳遞參數(shù)給POST請求。以下是一些常見的方式: 作為請求體:?你可以將參數(shù)作為請求體的一部分,通常用于發(fā)送表單數(shù)據(jù)或JSON數(shù)據(jù)。例如: 作為URL參數(shù):?你可以將參數(shù)作為URL的一部分,通常用于RESTful風(fēng)格的API。例如: 作為查詢字符串參數(shù):?你可以將

    2024年02月04日
    瀏覽(27)
  • Node.js GET/POST請求

    Node.js 中的 HTTP 模塊提供了創(chuàng)建 HTTP 服務(wù)器和發(fā)送 HTTP 請求的功能。在本文中,我們將探討如何使用 Node.js 發(fā)送 GET 和 POST 請求。 首先,您需要使用 http 模塊發(fā)送 GET 請求??梢允褂?http.get() 方法發(fā)送 GET 請求。它需要一個(gè)參數(shù),即請求的 URL。該方法返回一個(gè) http.ClientRequest 對

    2023年04月17日
    瀏覽(16)
  • OkHttpClient如何發(fā)get請求以及post請求

    加入依賴 寫代碼 2.1配置OkHttpClient 2.2請求參數(shù) 2.3請求頭配置 加入依賴 寫代碼 2.1配置OkHttpClient 2.2請求參數(shù) 2.3請求頭配置

    2024年02月13日
    瀏覽(28)
  • HTTP中g(shù)et和post請求方式

    #get和post特點(diǎn) get請求: 請求參數(shù)在請求地址后面,提交的數(shù)據(jù)量較小,安全性較差,不建議用來提交敏感信息(地址欄中會顯示,并且有可能被保存請求地址)。 功能:GET 方法用于獲取由 Request-URI 所標(biāo)識的資源的信息 默認(rèn)方法: GET方法是默認(rèn)的HTTP請求方法 ,例如當(dāng)我們

    2024年04月26日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包