亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Nodejs如何使用http標(biāo)準(zhǔn)庫(kù)異步加載https請(qǐng)求json數(shù)據(jù)

 更新時(shí)間:2022年09月28日 11:46:44   作者:江鳥(niǎo)木又源碼分析  
這篇文章主要介紹了Nodejs如何使用http標(biāo)準(zhǔn)庫(kù)異步加載https請(qǐng)求json數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

1. 同步方法實(shí)現(xiàn)

//通過(guò)https加載json數(shù)據(jù)
let url = 'https://img-home.csdnimg.cn/data_json/toolbar/toolbar1105.json';
let GetJsonData=function (url){
    const https = require('https');
    https.get(url, (response) => {
        let data = '';
        //數(shù)據(jù)正在接收中...
        response.on('data', (chunk) => {
            data += chunk;
        });
        //數(shù)據(jù)接收完成
        response.on('end', () => {
            console.log('同步請(qǐng)求數(shù)據(jù)完成:',JSON.parse(data));
        });
 
    }).on("error", (error) => {
        console.log("Error: " + error.message);
    });
}

加載數(shù)據(jù)

GetJsonData(url);

加載結(jié)果 

2.異步方法實(shí)現(xiàn)

//異步請(qǐng)求JSON數(shù)據(jù)實(shí)現(xiàn)
let GetJsonDataAsync=(url)=>{
    const https = require('https');
    return new Promise((resolve, reject) => {
        https.get(url, (response) => {
            let data = '';
            //數(shù)據(jù)正在接收中...
            response.on('data', (chunk) => {
                data += chunk;
            });
            //數(shù)據(jù)接收完成
            response.on('end', () => {
                //console.log(JSON.parse(data));
                resolve(data);//數(shù)據(jù)接收完成
            });
 
        }).on("error", (error) => {
            console.log("Error: " + error.message);
            reject(new Error(error.message));
        });
    });
};

加載結(jié)果處理

//異步調(diào)用
GetJsonDataAsync(url).then(value => {
    console.log("======================下面為異步加載數(shù)據(jù)=================================");
    if (typeof value === "string") {
        console.log('異步加載請(qǐng)求數(shù)據(jù)完成:', JSON.parse(value));
    }
})

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論