国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

vue fetch中的.then()的正確使用方法

瀏覽:3日期:2023-01-26 11:29:44

先看一段代碼:

fetch(’http://localhost:3000/books?id=123456’,{ method:’get’}).then(function(value1){ console.log(value1); return ’hello’;}).then(function(value2){ console.log(value2); return ’HelloWorld’;})/*.then(function(data){ console.log(data); return data.text(); })*/.then(data=>{ console.log(data);})

// 接口app.get(’/books’, (req, res) => { res.send(’傳統的URL傳遞參數!’ + req.query.id)})

vue fetch中的.then()的正確使用方法

在這段代碼中我們發現,最初傳入的是一個對象,緊接著后一個.then()的傳入參數使用了前一個.then()的返回值,換句話說,就是后一個then使用前一個then的封裝結果

那么現在去掉注釋:

vue fetch中的.then()的正確使用方法

.then(function(value2){ console.log(value2); return ’HelloWorld’;}).then(function(data){ console.log(data); return data.text(); })text()方法屬于fetch API的一部分,返回一個Promise實例對象,用于獲取后臺返回的數據

這段代碼中,傳入的data是上一步封裝的字符串,所以此時用data.text()報錯,除非data為對象

下面演示正確使用方式:

fetch(’http://localhost:3000/books?id=123456’,{ method:’get’}).then(function(data){ console.log(data); console.log(typeof(data)); return data.text();}).then(data=>{ console.log(data); console.log(typeof(data));})

vue fetch中的.then()的正確使用方法

輸出了接口詢問的內容,為String類型

到此這篇關于vue fetch中的.then()的正確使用方法的文章就介紹到這了,更多相關vue fetch .then()內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Vue
相關文章:
主站蜘蛛池模板: 平潭县| 台江县| 建水县| 东莞市| 纳雍县| 诏安县| 万荣县| 临猗县| 岳西县| 南木林县| 沛县| 荆州市| 叙永县| 开封县| 阿坝| 西盟| 浠水县| 微博| 始兴县| 克什克腾旗| 固安县| 宁国市| 南昌县| 桂阳县| 兴文县| 新邵县| 越西县| 久治县| 贺州市| 松溪县| 牙克石市| 隆尧县| 苍梧县| 大埔区| 临邑县| 广南县| 社会| 新建县| 开江县| 西安市| 财经|