axios跨域和一言api调用

本文最后更新于2022.05.14-16:05,某些文章具有时效性,若有错误或已失效,请在下方留言或联系涛哥

 1,在所在项目安装axios

npm install axios -s

import axios from 'axios';

2,在config下修改index.js文件在proxyTable里加入后端目标地址

index.js

'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.

const path = require('path')

module.exports = {
  dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {//代理服务器
      '/aaa':{
        target:"http://localhost:80",//API服务所在ip及端口号
        changeOrigin:true,
        pathRewrite:{
           '^/aaa':''
        }
      },
      '/api':{
        target:"https://v1.hitokoto.cn/?c=c&j=j&f=f&d=j&encode=text",//api接口                                                                              
        changeOrigin:true,
        pathRewrite:{
           '^/api':''
        }
      }
    },

    // Various Dev Server settings
    host: 'localhost', // can be overwritten by process.env.HOST
    port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    autoOpenBrowser: false,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-

    
    /**
     * Source Maps
     */

    // https://webpack.js.org/configuration/devtool/#development
    devtool: 'cheap-module-eval-source-map',

    // If you have problems debugging vue-files in devtools,
    // set this to false - it *may* help
    // https://vue-loader.vuejs.org/en/options.html#cachebusting
    cacheBusting: true,

    cssSourceMap: true
  },

  build: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',

    /**
     * Source Maps
     */

    productionSourceMap: true,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',

    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }
}

3,yiyan.vue

<template>
    <div>
			<button @click="getReq()">发送请求</button>
			<h1 v-if="Rts">请求内容 {{Rts}}</h1>
			<button @click="getInfo()">一句有哲理的话</button>
			<p v-if="msg">{{msg}}</p>
		</div>
</template>

<script>
//axios
// npm install axios -s
import axios from 'axios'
export default {
    name:'Rts',
	data(){
		return{
			Rts:'',
			msg:''
		}
	},
    methods:{
		getReq(){
			axios.get('http://localhost:8080/aaa/getUser.json')
			.then(res => {
				console.log("请求成功了",res.data);
				this.Rts=res.data;
			})
			.catch(err => {
				console.error("请求失败了"+err.err); 
			})
		},
		getInfo(){
			axios.get('http://localhost:8080/api')
			.then(res => {
				console.log("请求成功了",res.data);
				this.msg=res.data;
			})
			.catch(err => {
				console.error("请求失败了"+err.err); 
			})
		}
	}

}
</script>

<style scoped>
   
</style>

4,App.vue

<template>
  <div id="app">
       <Rts/>
  </div>
</template>

<script>
//引入组件
import Rts from './components/Rts.vue'

export default {
  name: 'App',
  components: {//注册组件
    Rts
  }
}
</script>

<style scoped>

</style>>

</style>

10-11Vue学习

 

阅读剩余
THE END