axios的get请求 - 代码篇

vue + elemetUI + axios的get方式请求(踩坑篇)

bug代码示例:

一、<script> · 部分代码
data() {
      return {
				resumes :[],
				xss: [],
				dialogShareFormVisible: false, // 弹框:新建分享 默认不显示
      }
},

// ================================================================================
// 异步请求:获取选项字典
ajaxOptionsOfShareForm: function (e,dialogShareFormVisible,xss){
	this.dialogShareFormVisible = true;
	var key = localStorage.getItem("token");
	console.log("获取选项字典"+key)

	var localPath = this.GLOBAL.localSrc;  // 本地 接口地址
    var serverPath = this.GLOBAL.serverSrc; // 线上 接口地址

	axios.get( serverPath + '/state',
		{
			params:{  // 注意1:
			
			},
			headers: { // 注意2:
				'Content-Type':'application/json',
				'Authorization': key
			}
		}
	)
	.then(function (response) {
		if (response.data.code == "200"){
			// 弹框成功提示
			this.$notify({
				title: response.data.message,
				type: 'success',
				duration: 2000
			});
		}

		localStorage.setItem("token",response.headers.authorization); // token 复写本地 localStorage					

		console.log("选项字典"+response.data.data.jobIndustry);

		this.xss = response.data.data.jobIndustry
	

	}.bind(this))
	.catch(function (error) {
			console.log("请求失败"+error);
	});

二、<templete> · 部分代码
<el-form-item label="意向职业" :label-width="labelWidth">
	<!-- <el-input v-model="form.jobIntention"  placeholder="例如:电话销售,网络销售,房产销售等" autocomplete="off"></el-input> -->
	<el-select v-model="form.jobIntention" placeholder="例如:电话销售,网络销售,房产销售等">
		<el-option v-for="(xs,index) in xss" :key="index" :label="xs.dictLabel" :value="xs.dictValue"></el-option>
	</el-select>
</el-form-item>

三、postman截图 · 部分API代码

在这里插入图片描述


相关阅读思考:

实际测试发现,这个方法是不行的。


具体参考项目get请求页面···

  1. searchMain.vue文件
	// 条件查询: 异步请求:获取选项字典
	axios.get( serverPath + '/state',
		{
			params:{

			},
			headers: {
				'Content-Type':'application/json',
				'Authorization': key
			}
		}
	)
	.then(function (response) {
		localStorage.setItem("token",response.headers.authorization); // token 复写本地 localStorage					

		console.log("选项字典"+response.data.data.jobIndustry);

		this.qzzts = response.data.data.jobWantedState // 求职状态
		this.xls = response.data.data.jobEducation // 学历

	}.bind(this))
	.catch(function (error) {
			console.log("请求失败"+error);
	});

  1. .XML文件(待补充)



以上就是关于“ axios的get请求 - 代码篇 ” 的全部内容。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用axios或node-fetch时,你可以封装一个常用的请求函数来简化代码和提高可重用性。以下是一个使用axios或node-fetch封装请求的示例: 使用axios封装请求的示例: ```javascript const axios = require('axios'); async function sendRequest(url, method, data) { try { const response = await axios({ method: method, url: url, data: data }); return response.data; } catch (error) { throw new Error(error.message); } } // 使用示例 sendRequest('https://api.example.com/users', 'GET') .then(data => { console.log(data); }) .catch(error => { console.error(error); }); ``` 使用node-fetch封装请求的示例: ```javascript const fetch = require('node-fetch'); async function sendRequest(url, method, data) { try { const response = await fetch(url, { method: method, body: JSON.stringify(data), headers: { 'Content-Type': 'application/json' } }); const responseData = await response.json(); return responseData; } catch (error) { throw new Error(error.message); } } // 使用示例 sendRequest('https://api.example.com/users', 'GET') .then(data => { console.log(data); }) .catch(error => { console.error(error); }); ``` 在上述示例中,我们封装了一个名为`sendRequest`的函数,该函数接受三个参数:URL、请求方法(GET、POST等)和请求数据(可选)。函数内部使用了axios或node-fetch来发送请求,并处理返回的响应数据。如果请求出现错误,将抛出一个包含错误消息的异常。 你可以根据自己的需求进一步扩展和定制这个封装函数,例如添加请求头、处理其他类型的响应数据等。 希望这个示例对你有帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值