标题: http客户端axios [打印本页] 作者: 杀鸡焉用牛刀 时间: 2024-7-12 02:35 标题: http客户端axios 1.nginx设置跨域
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
daemon off;
events {
worker_connections 65535;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - r e m o t e u s e r [ remote_user [ remoteuser[time_local] “$request” ’
'$status b o d y b y t e s s e n t " body_bytes_sent " bodybytessent"http_referer" ’
‘“ h t t p u s e r a g e n t " " http_user_agent" " httpuseragent""http_x_forwarded_for”’;
在利用时main.js需要导入axios组件。具体方式请参考下文。
// The Vue build version to load with the import command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from ‘vue’
import App from ‘./App’
import router from ‘./router’
import axios from “axios”
Vue.config.productionTip = false
Vue.prototype.$axios = axios
/* eslint-disable no-new */
new Vue({
el: ‘#app’,
router,
components: { App },
template: ‘’
})
复制代码
vue中的get请求.有两种写发,以下附上两种写法的格式。
{{data}}
复制代码
4.axios的post请求
由于假如调用的api前缀相同,那么可以利用全局设置,将url配玉成局,制止多次誊写。
这里需要时对main.js设置,以下附上代码。
// The Vue build version to load with the import command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from ‘vue’
import App from ‘./App’
import router from ‘./router’
import axios from “axios”
Vue.config.productionTip = false
Vue.prototype.$axios = axios
axios.defaults.baseURL = ‘https://api.example.com’;
axios.defaults.headers.common[‘Authorization’] = AUTH_TOKEN;
axios.defaults.headers.post[‘Content-Type’] = ‘application/x-www-form-urlencoded’;
/* eslint-disable no-new */
new Vue({
最后