目录
Vue结合网络数据开发应用
功能强大的网络请求库
-
-
-
- axios.get(地址?key1=value1&key2=value2).then(function(response){},function(err){})
- axios.post(地址{key1=value1,key2=value2}).then(function(response){},function(err){})
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
- <button class="get">get请求button>
- <button class="post">post请求button>
- <script src="https://unpkg.com/axios/dist/axios.min.js">script>
-
-
- <script>
- /* 随机笑话
- 请求地址:https//autumnfish.cn/api/joke/list
- 请求方法:get
- 请求参数:num
- 响应内容:随机笑话 */
- document.querySelector(".get").onclick = function () {
- axios.get("https://autumnfish.cn/api/joke/list?num=6").then(function (response) {
- console.log(response);
- }, function (err) {
- console.log(err);
- })
- }
-
- /* 用户注册
- 请求地址:https://autumnfish.cn/api/user/reg
- 请求方法:post
- 请求参数:username
- 响应内容:注册成功或失败 */
- document.querySelector(".post").onclick = function () {
- axios.post("https://autumnfish.cn/api/user/reg", { username: "jack" }).then(function (response) {
- console.log(response);
- }, function (err) {
- console.log(err);
- })
- }
- script>
- body>
-
- html>
文档传送门:GitHub - axios/axios: Promise based HTTP client for the browser and node.js
-
- <script src="https://unpkg.com/axios/dist/axios.min.js">script>
-
- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
- <div id="app">
- <button @click="getJoke">获取笑话button>
- <p>{{joke}}p>
- div>
-
- <script src="https://unpkg.com/axios/dist/axios.min.js">script>
-
- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
- <script>
- /* 接口:随机获取一条笑话
- 请求地址:https://autumnfish.cn/api/joke
- 请求方法:get
- 请求参数:无
- 响应内容:随机笑话 */
- var app = new Vue({
- el: "#app",
- data: {
- joke: "很好笑的笑话"
- },
- methods: {
- getJoke: function () {
- var that = this;
- axios.get("https://autumnfish.cn/api/joke").then(function (response) {
- that.joke = response.data;
- },
- function (err) {
- console.log(err);
- })
- }
- }
- })
- script>
- body>
-
- html>