使用axios发送请求

声明**:** 本文主要是照着官网教学自己对部分知识敲了一遍,纯属动手练习,没什么技术含量;更多细节、更多功能可详见https://github.com/axios/axios


axios发送请求的语法:

写法一(以get为例)**:**

  1. axios({
  2. method: 'get',
  3. url: 'https://github.com/axios/axios',
  4. data: {
  5. firstName: 'Fred',
  6. lastName: 'Flintstone'
  7. }
  8. //......
  9. }).then(response => {
  10. // handle success
  11. this.resultTwo.successInfo = response.data;
  12. }).catch(error => {
  13. // handle error
  14. this.resultTwo.failureInfo = error;
  15. }).then(() => {
  16. // always executed
  17. });

写法二(以get为例)**:**

  1. axios.get('https://github.com/axios/axios', {
  2. params: {
  3. firstName: 'Fred',
  4. lastName: 'Flintstone'
  5. }
  6. }).then(response => {
  7. // handle success
  8. this.resultTwo.successInfo = response.data;
  9. }).catch(error => {
  10. // handle error
  11. this.resultTwo.failureInfo = error;
  12. }).then(() => {
  13. // always executed
  14. });

简单示例:

" class="reference-link">提供三个可访问后端接口,以便前端进行请求测试:在这里插入图片描述

使用axios发送请求(示例):

  • 发送GET请求**:**

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>axios发送请求</title>
    7. <!-- 引入vue支持 -->
    8. <script src="../../../node.js/repository/node_modules/vue/dist/vue.js"></script>
    9. <!-- 引入axios支持 -->
    10. <script src="../../../node.js/repository/node_modules/_axios@0.19.2@axios/dist/axios.js"></script>
    11. <script> window.onload = function () { let vm = new Vue({ el: '#myId', data() { return { resultOne: { successInfo: '', failureInfo: '' }, resultTwo: { successInfo: '', failureInfo: '' } } }, methods: { getTestOne() { // 发送get请求, 参数直接手动拼接在url后面 axios.get('http://localhost:8080/abc?name=张三&motto=我不是黄蓉') .then((response) => { // handle success this.resultOne.successInfo = response.data; }).catch(error => { // handle error this.resultOne.failureInfo = error; }).then(() => { // always executed }); }, getTestTwo() { // 发送请求, 通过params拼接参数 axios.get('http://localhost:8080/abc', { params: { name: '邓沙利文', motto: '我是一只小小小小鸟~嗷!嗷!' } }).then(response => { // handle success this.resultTwo.successInfo = response.data; }).catch(error => { // handle error this.resultTwo.failureInfo = error; }).then(() => { // always executed }); } } }); } </script>
    12. </head>
    13. <body>
    14. <div id="myId">
    15. <button @click="getTestOne">axios发送get请求(测试一)</button>
    16. <p>{
    17. {resultOne.successInfo}}</p>
    18. <p>{
    19. {resultOne.failureInfo}}</p>
    20. <button @click="getTestTwo">axios发送get请求(测试二)</button>
    21. <p>{
    22. {resultTwo.successInfo}}</p>
    23. <p>{
    24. {resultTwo.failureInfo}}</p>
    25. </div>
    26. </body>
    27. </html>

    测试一下:
    在这里插入图片描述

  • 发送POST + JSON请求** 提示**: 默认的,axios调用post等方法时进行请求时,ContentType为application/json;charset=UTF-8

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>axios发送请求</title>
    7. <!-- 引入vue支持 -->
    8. <script src="../../../node.js/repository/node_modules/vue/dist/vue.js"></script>
    9. <!-- 引入axios支持 -->
    10. <script src="../../../node.js/repository/node_modules/_axios@0.19.2@axios/dist/axios.js"></script>
    11. <script> window.onload = function () { let vm = new Vue({ el: '#myId', data() { return { result: { successInfo: '', failureInfo: '' } } }, methods: { postJsonTest() { // 发送(请求内容为json的)post请求 axios.post('http://localhost:8080/xyz', { name: '邓沙利文', age: 26 }).then((response) => { // handle success this.result.successInfo = response.data; }).catch(error => { // handle error this.result.failureInfo = error; }).then(() => { // always executed }); } } }); } </script>
    12. </head>
    13. <body>
    14. <div id="myId">
    15. <button @click="postJsonTest">axios发送post + json请求(测试)</button>
    16. <p>{
    17. {result.successInfo}}</p>
    18. <p>{
    19. {result.failureInfo}}</p>
    20. </div>
    21. </body>
    22. </html>

    测试一下:
    在这里插入图片描述

  • 发送POST + Form表单请求** 提示**: 可以借助其它插件/类库来完成参数转换的功能。

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>axios发送请求</title>
    7. <!-- 引入vue支持 -->
    8. <script src="../../../node.js/repository/node_modules/vue/dist/vue.js"></script>
    9. <!-- 引入axios支持 -->
    10. <script src="../../../node.js/repository/node_modules/_axios@0.19.2@axios/dist/axios.js"></script>
    11. <script> window.onload = function () { let vm = new Vue({ el: '#myId', data: { resultOne: { successInfo: '', failureInfo: '' }, resultTwo: { successInfo: '', failureInfo: '' }, person: { name: '邓沙利文', hobby: '游戏、台球、撸代码' } }, methods: { /* * 参数本身就是形如【k1=v1&k2=v2】这样的形式 */ postJsonTestOne() { // 发送(请求内容为form表单的)post请求 axios.post( // url 'http://localhost:8080/qwer', // data 'name=JustryDeng&hobby=吃海鲜' ).then((response) => { // handle success this.resultOne.successInfo = response.data; }).catch(error => { // handle error this.resultOne.failureInfo = error; }).then(() => { // always executed }); }, /* * 参数本身是对象,使用transformRequest将其转换为形如【k1=v1&k2=v2】这样的形式 */ postJsonTestTwo() { // 发送(请求内容为form表单的)post请求 axios.post('http://localhost:8080/qwer', this.person, { // transformRequest里面会将请求参数(即:这里的this.person)进行转换,转换为'name=JustryDeng&hobby=吃海鲜'的形式 transformRequest: [ function (data) { let params = ''; for (let key in data) { params += key + '=' + data[key] + '&'; } if (params.length > 0) { // 去除最后的那个& params = params.substring(0, params.length - 1) } // console.log(params); return params; } ] }).then((response) => { // handle success this.resultTwo.successInfo = response.data; }).catch(error => { // handle error this.resultTwo.failureInfo = error; }).then(() => { // always executed }); } } }); } </script>
    12. </head>
    13. <body>
    14. <div id="myId">
    15. <button @click="postJsonTestOne">axios发送post + form请求(测试一)</button>
    16. <p>{
    17. {resultOne.successInfo}}</p>
    18. <p>{
    19. {resultOne.failureInfo}}</p>
    20. <button @click="postJsonTestTwo">axios发送post + form请求(测试二)</button>
    21. <p>{
    22. {resultTwo.successInfo}}</p>
    23. <p>{
    24. {resultTwo.failureInfo}}</p>
    25. </div>
    26. </body>
    27. </html>

    测试一下:
    在这里插入图片描述

再次声明 本文只是按照https://github.com/axios/axios所述,敲的非常简单、非常基础的demo,更多细节、更多功能可详见该地址。

^_^ 如有不当之处,欢迎指正

^_^ 学习链接
https://github.com/axios/axios

^_^ 本文已经被收录进《程序员成长笔记(六)》,笔者JustryDeng

发表评论

表情:
评论列表 (有 0 条评论,54人围观)

还没有评论,来说两句吧...

相关阅读