博客
关于我
第十章:vue2中axios请求服务端数据
阅读量:132 次
发布时间:2019-02-26

本文共 1597 字,大约阅读时间需要 5 分钟。

一、关于axios的安装

  • 1、利用npm安装npm install axios --save
  • 2、利用bower安装bower install axios --save
  • 3、直接利用cdn引入<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

二、关于get请求数据

  • 1、直接请求数据

    axios.get('/static/data.json').then(res=>{    console.log(res);    ....}).catch(error=>console.log(error));
  • 2、带参数的请求方式

    axios.get('/static/data.json',{params:{id:123}}).then(res=>{    console.log(res);    ....}).catch(error=>console.log(error));

二、关于post请求数据

  • 1、直接请求

    axios.post('/user', {    firstName: 'Fred',    lastName: 'Flintstone'}).then(function (response) {         console.log(response);}).catch(function (error) {         console.log(error);});

三、vue-cli构建项目中利用axios请求数据处理跨域的问题

  • 1、vue-cli构建项目
  • 2、服务器数据接口

    'use strict';const express = require("express");const app = express();let obj = {    info:'success',    code:200,    booknames:[        {id:0,bookname:'三国演义',author:'罗贯中'},        {id:1,bookname:'水浒传',author:'施耐庵'},        {id:2,bookname:'西游记',author:'吴承恩'},        {id:3,bookname:'红楼梦',author:'曹雪芹'},    ]};app.get('/',(req, res) => {    res.json(obj);})app.get('/book',(req, res) => {    res.json(obj);})app.listen(3000);
  • 3、设置代理(在config->index.js)中

    // 配置代理  proxyTable: {    '/api':{  // api为匹配项        target:'http://localhost:3000/', // 设置代理目标        changeOrigin: true,        pathRewrite: {  // 重写路径          '^/api': '/'        }    }},
  • 4、请求数据

    import axios from 'axios';export default {    name: 'app',    mounted(){        axios.get('/api/book').then(res=>{            console.log('第一条数据:',res);        })        axios.get('/api').then(res=>{            console.log('第二条数据:',res);        })    }}

转载地址:http://aavf.baihongyu.com/

你可能感兴趣的文章
PowerDesigner165安装婆姐汉花教程
查看>>
PowerDesigner使用教程:设置注释、默认值属性
查看>>
PowerDesigner使用教程:不显示背景网格
查看>>
PowerDesigner使用教程:创建数据模型以及导出
查看>>
PowerDesigner使用教程:右侧工具栏显示/隐藏
查看>>
PowerDesigner使用教程:导出sql文件以及解决中文乱码问题
查看>>
PowerDesigner使用教程:时间字段设置
查看>>
PowerDesigner使用教程:给字段添加唯一约束
查看>>
QGIS中怎样设置图层样式并导出地图样式
查看>>
PowerDesigner使用笔记
查看>>
QGIS中怎样实现数据坐标系转换
查看>>
PowerDesigner学习--基本步骤
查看>>
PowerDesigner导出Report通用报表
查看>>
PowerDesigner教程系列(二)概念数据模型
查看>>
Powerdesigner显示表的comment和列的comment的方法
查看>>
PowerDesigner最基础的使用方法入门学习
查看>>
PowerDesigner版本控制器设置权限
查看>>
PowerDesigner生成数据模型并导出报告
查看>>
QGIS中导入dwg文件并使用GetWKT插件获取绘制元素WKT字符串以及QuickWKT插件实现WKT显示在图层
查看>>
PowerDesigner逆向工程从SqlServer数据库生成PDM(图文教程)
查看>>