vue3?HTTP請(qǐng)求中的axios示例詳解
vue3-HTTP請(qǐng)求
背景
vue本身不支持發(fā)送AJAX請(qǐng)求,需要使用vue-resource、axios等插件實(shí)現(xiàn)。
axios是一個(gè)基于Promise的HTTP請(qǐng)求客戶端,用來發(fā)送請(qǐng)求,也是vue2.0官方推薦的,同時(shí)不再對(duì)vue-resource進(jìn)行更新和維護(hù)。
axios
官網(wǎng): https://axios-http.com/
github:https://github.com/axios/axios
Axios 是一個(gè)簡(jiǎn)單的基于 promise 的 HTTP 客戶端,適用于瀏覽器和 node.js。Axios 在具有非??蓴U(kuò)展的接口的小包中提供了一個(gè)簡(jiǎn)單易用的庫。
安裝axios并引入
安裝:
npm的方式:
npm install axios --save
引入,【在哪里使用,就在哪里引入】
import axios from 'axios';
使用demo:
main.js
import Vue from 'vue' import App from './App.vue' Vue.config.productionTip = false new Vue({ render: h => h(App), }).$mount('#app')
App.vue
<template> <div> <div v-if="!repositoryUrl">loading...</div> <div v-else>most star repository is <a :href="repositoryUrl" rel="external nofollow" >{{repositoryName}}</a></div> </div> <!--App --> </template> <script> import axios from 'axios'; export default { data() { return { repositoryUrl : '', repositoryName : '' } }, mounted() { // 發(fā)ajax請(qǐng)求,用以獲取數(shù)據(jù),此處地址意思是查詢 github中 vue 星數(shù)最高的項(xiàng)目 const url = 'https://api.github.com/search/repositories?q=vue&sort=stars'; axios.get(url).then( response => { const result = response.data.items[0]; console.log(result) this.repositoryUrl = result.html_url; this.repositoryName = result.name; } ).catch( response => { alert('請(qǐng)求失敗'); }, ); } } </script> <style> </style>
axios POST提交數(shù)據(jù)
Content-Type: application/json
const url = '/api/v1/web3/url/list_by_category'; let data = {"category":"tools"}; axios.post(url,data).then( response => { const result = response.data.data; console.log(result) this.repositoryName = result.name; this.web3_urls = result }).catch(response => { alert('請(qǐng)求失敗'); }, );
工作中遇到常見問題
has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource
cors阻止了你請(qǐng)求的資源(跨域問題)
解決方案:
在vue3.0中解決跨域首先要配置vue.config.js(在根目錄下創(chuàng)建vue.config.js、跟package.json同級(jí)的地方)
vue.config.js
在vue.config.js中加入以下代碼
module.exports = { devServer: { proxy: { '/api': { target: 'https://www.xxx.com/', //接口域名 changeOrigin: true, //是否跨域 ws: true, //是否代理 websockets secure: true, //是否https接口 pathRewrite: { //路徑重置 '^/api': '' } } } } };
我用的vite,參考
vue3(vite)設(shè)置代理,封裝axios,api解耦
參考URL: http://www.dbjr.com.cn/article/271024.htm
官方:https://vitejs.dev/config/server-options.html
我們修改的是vite.config.js,內(nèi)容如下,核心就是加入了 server–> proxy字段:
import { fileURLToPath, URL } from 'node:url' import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' // https://vitejs.dev/config/ export default defineConfig({ plugins: [vue()], resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } }, server: { proxy: { '/api': { target: 'http://127.0.0.1:8000/', //接口域名 changeOrigin: true, //是否跨域 ws: false, //是否代理 websockets secure: false, //是否https接口 pathRewrite: { //路徑重置 '^/api': '' } } } } })
參考文獻(xiàn)
vue3(vite)設(shè)置代理,封裝axios,api解耦
參考URL: http://www.dbjr.com.cn/article/271024.htm
到此這篇關(guān)于vue3-HTTP請(qǐng)求之a(chǎn)xios的文章就介紹到這了,更多相關(guān)vue3 axios請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Vue項(xiàng)目跨平臺(tái)運(yùn)行問題的解決方法
這篇文章主要介紹了關(guān)于Vue項(xiàng)目跨平臺(tái)運(yùn)行問題的解決方法,特別記錄一下踩的坑,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09Vue移動(dòng)端下拉加載更多數(shù)據(jù)onload實(shí)現(xiàn)方法淺析
這篇文章主要介紹了Vue移動(dòng)端下拉加載更多數(shù)據(jù)onload實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02vue基于element-china-area-data插件實(shí)現(xiàn)省市區(qū)聯(lián)動(dòng)
省市區(qū)聯(lián)動(dòng)在日常開發(fā)中用的非常多,本文就介紹一下vue基于element-china-area-data插件實(shí)現(xiàn)省市區(qū)聯(lián)動(dòng),具有一定的參考價(jià)值,感興趣的可以了解一下2022-04-04vue實(shí)現(xiàn)移動(dòng)端input上傳視頻、音頻
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)移動(dòng)端input上傳視頻、音頻,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08Vue 配合eiement動(dòng)態(tài)路由,權(quán)限驗(yàn)證的方法
今天小編就為大家分享一篇Vue 配合eiement動(dòng)態(tài)路由,權(quán)限驗(yàn)證的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09