基于vue-cli的vue项目之axios的使用3--get传参请求

本文详细介绍如何在Vue项目中通过配置解决跨域问题,并演示了如何利用axios发送GET请求来获取数据。从配置文件调整到请求参数设置,一步步解析跨域请求的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以get传参请求为例子,

1.配置config/index.js:解决跨域问题
dev: {
		env: require('./dev.env'),
		port: 8008,
		autoOpenBrowser: false,
		assetsSubDirectory: 'static',
		assetsPublicPath: '/',
		proxyTable: {
			'/ajaxurl': {
				target: 'https://www.aaaaaaa.com/',
				changeOrigin: true,
				pathRewrite: {
					'^/ajaxurl': '/'
				}
			}

		}
	
	}

2.main.js:配置axios到原型链中,注意第二十五行
import Vue from 'vue'
import App from './App'
import axios from 'axios'
Vue.prototype.$http=axios;
new Vue({
	el: '#app',
	render: h => h(App)
})

3.app.vue:使用请求,get为例,第四十六行设置参数
<template>
	<div id="app">
		huoqu
		<button @click="myajax">获取首页信息</button>
	</div>
</template>
<script>
	export default {
		name: 'app',
		components: {},
		data: function() {},
		methods: {
			//get为例子
			myajax: function() {
				this.$http.get("/ajaxurl/welfare/gpa/brand/list", {params: {page: 1,size: 10}})
				.then(response => {
					console.log("获取信息成功");
					console.log(response);
				}, response => {
					console.log("获取信息失败");
					console.log(response);
				})

			}
		}
	}
</script>
<style>

</style>

效果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值