一、引入axios资源库

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

在这里插入图片描述

二、事例代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>axios 网络请求库</title>
		<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
		<!-- 引入axios资源库 -->
		<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
	</head>
	<body>
		<div id="app">
			<input type="button" value="获取笑话" @click="getjoke" />
			<p> {{joke}}</p>
		</div>
		<script type="text/javascript">
			var app = new Vue({
				el: "#app",
				data: {
					joke: "这是一条笑话",
				},
				methods: {
					getjoke: function() {
						var that = this;
						axios.get("https://autumnfish.cn/api/joke").then(function(response) {
							console.log(response.data);
							that.joke = response.data;
						}, function(err) {})
					}
				},
			})
		</script>
	</body>
</html>

三、总结

在这里插入图片描述

Logo

前往低代码交流专区

更多推荐