安装$ npm install axios

1.发送get请求:

axios.get("/api/v1.0/cars?id=132").then(function(res){

console.log(res)

}).catch(function(err){

console.log(err)

});

2.发送post请求:

let params = {

id:4, ctime:'2019-03-1',name:"奔驰4"

}

//'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'

axios.post('/api/v1.0/cars',params,{

 headers: {

            'Content-Type':'application/json'

          }

}).then(res=>{

console.log(res.data)

},err =>{

console.log(err)

})

},

使用flask模拟后台提供接口,只需要修改config/index.js     下的配置项

proxyTable: {

'/api':'http://localhost:5000/',

},

flask 服务器端接口定义:

@app.route('/api/v1.0/cars', methods=['GET','POST'])

def get_cars():

if request.method == 'POST':

car = request.get_json() #接受post提交的参数

cars.append(car)

print(car)

return jsonify({'ok':'ok'})

else:

id = request.args.get("id") #接受get请求的参数

if id:

print("id:", id)

for item in cars:

if id == item.get("id"):

return jsonify({'cars': item})

return jsonify({'cars': "id 为" + id + "的车不存在!"})

else:

return jsonify({'cars': cars})

3.put 请求

axios.put("/api/v1.0/cars?id=1&name=宝马x").then(function(res){

console.log(res)

}).catch(function(err){

console.log(err)

});

flask 服务器端接受put请求处理:

@app.route('/api/v1.0/cars', methods=['PUT'])

def put_cars():

"""更新指定id的car的name信息"""

id = request.args.get("id")

name = request.args.get("name")

print(request.args)

for item in cars:

if int(id) == item.get("id"):

item['name'] = name

return jsonify({'cars': item})

return jsonify({'cars': "id 为" + id + "的车不存在!"})

4.delete请求

axios.delete("/api/v1.0/cars?id=2").then(function(res){

console.log(res)

}).catch(function(err){

console.log(err)

});

flask 服务器端接受delete请求处理

@app.route('/api/v1.0/cars', methods=['DELETE'])

def delete_cars():

"""删除指定id的car信息"""

id = request.args.get("id")

print(request.args)

for item in cars:

if int(id) == item.get("id"):

cars.remove(item)

return jsonify({'errno': 0, 'errmsg':  "delete ok"})

return jsonify({'cars': "id 为" + id + "的车不存在!"})

5.get请求路径作为参数时

axios.get("/api/v1.0/cars/3").then(function(res){

console.log(res)

}).catch(function(err){

console.log(err)

});

flask处理

@app.route('/api/v1.0/cars/<int:id>', methods=['GET'])

def get_cars_by_id(id):

for item in cars:

if int(id) == item.get("id"):

return jsonify({'cars': item})

return jsonify({'cars': "id 为" + id + "的车不存在!"})

6.axios文件上传:

<form>

<input type="text" value="" v-model="name" placeholder="请输入用户名">

<input type="text" value="" v-model="age" placeholder="请输入年龄">

<input type="file" @change="getFile($event)">

<button @click="submitForm($event)">提交</button>

</form>

data:function(){

return{

name:'',

age:'',

file:''

}

},

getFile(event) {

this.file = event.target.files[0];

console.log(this.file);

},

submitForm(event) {

event.preventDefault();

let formData = new FormData();

formData.append('name', this.name);

formData.append('age', this.age);

formData.append('file', this.file);

console.log(formData)

let config = {

headers: {

'Content-Type': 'multipart/form-data'

}

}

axios.post('/api/v1.0/upload', formData, config).then(res=>{

console.log(res);

}).catch(err=>{

console.log(err)

})

},

flask服务器端接受

@app.route('/api/v1.0/upload', methods=['GET', 'POST'])

def upload_file():

upload_time = time.strftime("%Y%m%d%H%M%S", time.localtime())

name = request.form.get("name")

age = request.form.get("age")

print("name:" + name + ",age:" + age)

f = request.files['file']

f.save("upload/" + upload_time + f.filename)

return jsonify({'msg': "ok"})

axiso基本使用及python接收处理的更多相关文章

  1. Python接收邮件并保存至MySQL

    转自:http://www.360doc.com/content/14/0103/13/11789990_342303735.shtml 参考了一些网络上的资料,做了个简单程序,使用python接收邮 ...

  2. python接收字符并回显

    # -*- coding: utf-8 -* import serial import time # 打开串口 ser = serial.Serial("/dev/ttyAMA0" ...

  3. python接收axios的post请求,并处理后返回数据

    公司的python工程师不会js和python数据交互,所以我就去试了一下. 首先安装python,django框架和django-cors-headers. python官网下载,按提示操作,记住最 ...

  4. python接收图片变成缩略图

    python图像处理库:Pillow初级教程 Image类 Pillow中最重要的类就是Image,该类存在于同名的模块中.可以通过以下几种方式实例化:从文件中读取图片,处理其他图片得到,或者直接创建 ...

  5. python接收邮件

    # -*- coding: utf-8 -*- import poplib import email from email.parser import Parser from email.header ...

  6. python接收html页面上传的文件

    使用的 flask, 没有安装的先安装 pip install flask 示例代码:示例没有自动创建静态文件夹,需要自己在同级 创建一个名为 static 的文件夹来存放上传的文件 示例展示为图片 ...

  7. Python接收执行参数

    1.python内置模块sys的sys.argv函数会将解释器执行时的所有参数保存为一个列表. a.首先创建文件login.py #!/usr/bin/env python #-*- coding:u ...

  8. Python学习--01入门

    Python学习--01入门 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.和PHP一样,它是后端开发语言. 如果有C语言.PHP语言.JAVA语言等其中一种语言的基础,学习Py ...

  9. python之最强王者(2)——python基础语法

    背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...

随机推荐

  1. 几句简单的python代码完成周公解梦功能

    <周公解梦>是靠人的梦来卜吉凶的一本于民间流传的解梦书籍,共有七类梦境的解述.这是非常传统的中国文化体系的一部分,但是如何用代码来获取并搜索周公解梦的数据呢?一般情况下,要通过爬虫获取数据 ...

  2. 在Python中使用glob模块查找文件路径的方法

    在Python中使用glob模块查找文件路径的方法 glob模块是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用windows下的文件搜索差不多.查找文件只用到三个匹配符: ...

  3. CentOS7或CentOS8 开机自动启用网卡的设置方法

    sudo nano /etc/sysconfig/network-scripts/ifcfg-enp0s3(p0s3是网卡,名字不同环境会有差异,输入时可按tab自动补全.) 将最后一行的 ONBOO ...

  4. CentOS里查看内存的使用

    转自 http://blog.csdn.net/blueman2012/article/details/6904597

  5. 为什么单个TCP连接很难占满带宽

    计算 TCP吞吐量的公式 TCP窗口大小(bits) / 延迟(秒) = 每秒吞吐量(bits) 比如说windows系统一般的窗口大小为64K, 中国到美国的网络延迟为150ms. 64KB = 6 ...

  6. JS&和&&-T

    &&逻辑与 &按位与(转换为二进制运算) console.log(1&2); console.log(1&&2); 上面打印的结果是什么呢? 先复习一下 ...

  7. mysql对数据的操作

    增: insert into 表名(字段,字段,字段) values(值,值,值): insert into 表名 values(值,值,值): insert into 表名 set 字段=值, 字段 ...

  8. 使用TensorFlow训练SSD(二):数据的准备

    在进行模型的训练之前,需要准备好相关的数据,相关的数据还需要进行标注.这篇博客将使用labelImg标注工具来进行数据的处理. 首先可从https://github.com/tzutalin/labe ...

  9. 生产环境Crontab专业实例

    1)书写Crontab定时任务多个基本要领 1.规范定时任务两例 例1:每分钟打印一次自己名字的全拼到 “/server/log/自己命名的文件” 中

  10. CWMP开源代码研究——git代码工程

    原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 声明:本系列涉及的开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅 ...