博客
关于我
第十章: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/

你可能感兴趣的文章
R730服务器做了raid的硬盘,插在R720上面可以用吗?
查看>>
PHP类数组式访问(ArrayAccess接口)
查看>>
PHP系列:浅谈PHP中isset()和empty() 函数的区别
查看>>
PHP索引数组unset的坑-array_values解决方案
查看>>
PHP索引数组排序方法整理(冒泡、选择、插入、快速)
查看>>
PHP线程安全和非线程安全
查看>>
R3LIVE开源项目常见问题解决方案
查看>>
php缃戠珯,www.wfzwz.com
查看>>
php缓存查询函数
查看>>
php编写TCP服务端和客户端程序
查看>>
php编码规范
查看>>
PHP编码规范-PSR1、psr2 /psr3 psr4
查看>>
PHP编程效率的20个要点
查看>>
PHP网页缓存技术优点及代码
查看>>
PHP自动化测试(一)make test 和 phpt
查看>>
php自定义函数: 文件大小转换成智能形式
查看>>
php英语单词,php常用英语单词,快速学习php编程英语(6)
查看>>
R3.4.0安装包时报错“需要TRUE/FALSE值的地方不可以用缺少值”,需升级到R3.5.0
查看>>
PHP获取curl传输进度
查看>>
PHP获取IP所在地区(转)
查看>>