json数据对接
1.前言
- fastadmin框架本身封装了一系列接口和插件来对表格数据进行管理(新增,编辑,删除),但是其使用的bootstrapTable基于jquery开发,基于某些原因,我们想要使用Vue框架代替bootstrapTable对页面进行渲染,但是会涉及参数的数据格式问题,此文对想一个的数据格式进行测试,方便后续使用
2.请求头
- 请求头要设定两个字段:content-type 和 X-Requested-With
const result = await this.$axios({
url:"/admin/sp/mcsparepart/add",
headers:{
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",//数据格式申明
'X-Requested-With': 'XMLHttpRequest'
},
method:"post",
data:formData
})
3.参数的数据格式
vue中一般使用json数据,而fastadmin提供的接口使用的是表单数据格式,在转换过程中要注意两点:
- json和form两种格式区别如下:
var jsonData = {
key1:"1",key2:"2"
}
var formData = "key1=1&key2=2"
- 直接将json数据转换为表单字符串(用于get请求)
//将json数据转换成表单字符串
jsonToFormData(data){
var formData = ""
for(var key in data){
formData += `${key}=${data[key]}&`
}
//去除最后一个字母&
formData = formData.substring(0,formData.length-1)
return formData
}
- 将json数据转换成表单对象(用于post请求)
//创建新的表单
var form = new FormData()
//遍历json,给表单添加字段
for(var key in json){
form.append(`row[${key}]`,json[key])
}
//返回表单
return form
4.对接表格列表接口
- 表格列表接口的地址:页面地址
- 请求方式:get
- 需要设定请求头
headers:{
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",//数据格式申明
'X-Requested-With': 'XMLHttpRequest'
}
- 基本参数(已解析视图):分页
offset: 10,limit: 10
- 搜索过滤参数(已解析视图):
//搜索字段的值(json字符串)
filter: "{downmenu_value: value}"
//模糊搜索(LIKE)还是精确搜索(=)
op: "{downmenu_value:'LIKE'}"
- 配合axios使用
//this.current_page当前页码
//this.page_size每页数量
//this.op字段搜索方式 {key:"Like",key2:"="}
//search_params搜索表单(过滤无数据的字段)
const result = await this.$axios({
url:"/admin/warehouse/warehouse",
headers:{
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",//数据格式申明
'X-Requested-With': 'XMLHttpRequest'
},
params:{
offset: (this.current_page-1) * this.page_size,//跳过条数
limit: this.page_size,//获取条数
op: JSON.stringify(this.op),//字段搜索方式
filter: JSON.stringify(search_params),//搜索字段值
}
})
5.对接新增接口和编辑接口
- 新增接口的地址:页面地址 + /add?dialog=1
- 编辑接口的地址(ids为要编辑行的id):页面地址 + /edit/ids/"+ids
- 请求方式:post
- post参数:post参数表单格式,每个参数名称外面需要包裹一层row,例如:
//创建新的表单
var form = new FormData()
//遍历json,给表单添加字段
for(var key in json){
form.append(`row[${key}]`,json[key])
}
//返回表单
return form
6.对接删除接口
- 删除接口的地址:页面地址 + /del
- 请求方式:post
- post参数(指定action字段和ids字段,如果有多个id,则用逗号分割):action=del&ids=180
var formData = action=del&ids=180
const result = await this.$axios({
url:"/admin/sp/mcsparepart/del",
headers:{
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
'X-Requested-With': 'XMLHttpRequest'
},
method:"post",
data:formData
})
7.单独修改某个字段值
- 接口地址:页面地址 + /multi
- 请求方式:post
- 参数:
ids: 1
params: status=0
8.完整代码
- 获取表格数据
//发请求 获取表格数据
async getTableData(){
try {
//过滤出搜索表单没有值的字段
var search_params = {}
for(var key in this.form){
var value = this.form[key]
if(value){
search_params[key] = value
}
}
//表格加载状态
this.table_loading = true
//发请求
const result = await this.$axios({
url:"/admin/warehouse/warehouse",
headers:{
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",//数据格式申明
'X-Requested-With': 'XMLHttpRequest'
},
params:{
offset: (this.current_page-1) * this.page_size,//跳过条数
limit: this.page_size,//获取条数
op: JSON.stringify(this.op),//字段搜索方式
filter: JSON.stringify(search_params),//搜索字段值
}
})
//表格加载状态
this.table_loading = false
//表格加载状态
this.tableData = result.data.rows || []
this.total = result.data.total
} catch (error) {
console.log(error)
//表格加载状态
this.table_loading = false
}
},
- 新增请求
//发请求 新增数据
async submitAdd(form){
try {
//提交loading状态
this.submit_loading = true
//发请求
const result = await this.$axios({
url:"/admin/warehouse/warehouse/add",
method:"post",
headers:{
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",//数据格式申明
'X-Requested-With': 'XMLHttpRequest'
},
data:form
})
//提交loading状态
this.submit_loading = false
//处理返回结果
if (result.data.code == 1) {
//关闭弹窗
this.dialogVisible = false
//重置页码
this.current_page = 1
//刷新表格数据
this.getTableData()
} else {
this.$message.error(result.data.msg)
}
} catch (error) {
console.log(error)
//提交loading状态
this.submit_loading = false
}
},
- 编辑请求
async submitEdit(form){
try {
//提交loading状态
this.submit_loading = true
//发请求
const result = await this.$axios({
url:"/admin/warehouse/warehouse/edit/ids/" + this.edit_id,
method:"post",
headers:{
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",//数据格式申明
'X-Requested-With': 'XMLHttpRequest'
},
data:form
})
//提交loading状态
this.submit_loading = false
//处理返回结果
if (result.data.code == 1) {
//关闭弹窗
this.dialogVisible = false
//重置页码
this.current_page = 1
//刷新表格数据
this.getTableData()
} else {
this.$message.error(result.data.msg)
}
} catch (error) {
console.log(error)
//提交loading状态
this.submit_loading = false
}
},
- 删除请求
//发请求 删除数据
async submitDelete(ids){
var formData = `action=del&ids=${ids}`
try {
this.table_loading = true
const result = await this.$axios({
url:"/admin/warehouse/warehouse/del",
method:"post",
data: formData,
headers:{
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",//数据格式申明
'X-Requested-With': 'XMLHttpRequest'
}
})
this.table_loading = false
//处理返回结果
if (result.data.code == 1) {
//关闭弹窗
this.dialogVisible = false
//刷新表格数据
this.getTableData()
} else {
this.$message.error(result.data.msg)
}
} catch (error) {
console.log(error)
this.table_loading = false
}
}
json数据对接的更多相关文章
- 通过AngularJS实现前端与后台的数据对接(二)——服务(service,$http)篇
什么是服务? 服务提供了一种能在应用的整个生命周期内保持数据的方法,它能够在控制器之间进行通信,并且能保证数据的一致性. 服务是一个单例对象,在每个应用中只会被实例化一次(被$injector实例化) ...
- 格式化JSON数据
function formatJson(json, options) { var reg = null, formatted = '', pad = 0, PADDING = ' '; options ...
- C# 读取网页JSON数据
场景描述: 公司和别的系统需要对接,现在对方提供一个网址,数据都是json字符串,我需要对json数据进行处理. 提供的json数据如下格式 一.读取网址中的json数据 public string ...
- python 全栈开发,Day94(Promise,箭头函数,Django REST framework,生成json数据三种方式,serializers,Postman使用,外部python脚本调用django)
昨日内容回顾 1. 内容回顾 1. VueX VueX分三部分 1. state 2. mutations 3. actions 存放数据 修改数据的唯一方式 异步操作 修改state中数据的步骤: ...
- [系列] Go 如何解析 JSON 数据?
概述 最近掉进需求坑了,刚爬上来,评估排期出现了严重问题,下面三张图很符合当时的心境. 谈需求 估排期 开始干 为啥会这样,我简单总结了下: 与第三方对接. 跨团队对接. 首次用 Go 做项目. 业务 ...
- 数据对接:从Notion Database到低代码平台
前言 Notion简介 近几年,有一款叫Notion的产品异常火爆,它是集笔记.任务管理.Wiki.数据管理为一体的产品,他主打两个理念「模块化」和「All-in-one」,Notion最有魅力的还是 ...
- 使用TSQL查询和更新 JSON 数据
JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...
- 通过AngularJS实现前端与后台的数据对接(一)——预备工作篇
最近,笔者在做一个项目:使用AngularJS,从而实现前端与后台的数据对接.笔者这是第一次做前端与后台的数据对接的工作,因此遇到了许多问题.笔者在这些问题中,总结了一些如何实现前端与后台的数据对接的 ...
- 利用Python进行数据分析(2) 尝试处理一份JSON数据并生成条形图
一.JSON 数据准备 首先准备一份 JSON 数据,这份数据共有 3560 条内容,每条内容结构如下: 本示例主要是以 tz(timezone 时区) 这一字段的值,分析这份数据里时区的分布情况. ...
- Salesforce Apex 使用JSON数据的示例程序
本文介绍了一个在Salesforce Apex中使用JSON数据的示例程序, 该示例程序由以下几部分组成: 1) Album.cls, 定了了封装相关字段的数据Model类 2) RestClient ...
随机推荐
- ASP.NET Core – Minimal API
介绍 Minimal API 是 .NET 6 才开始有的功能. 它是一个简化版本的 Web API. 我还没有认真的去学习它, 感觉它走的是 Node.js Express 的路线. 目前用它来写小 ...
- TypeScript – tsconfig
前言 上一篇 TypeScript – Get Started 使用了命令 tsc index.ts --module es2015 很少人会在命令时给写 config, 更正规的做法是创建一个 ts ...
- Asp.net core 学习笔记 Image processing (ImageSharp)
请移步修订版 : ASP.NET Core Library – ImageSharp .net 的生态烂是真的, 很多硬需求都没有人做, 开源的做着做着就闭源了的也很多. 今天说说 image pro ...
- pimp技法浅析--实现轻量级的面向接口编程
pimp.hpp: #ifndef pimp_hpp #define pimp_hpp class CMyComponent{ public: CMyComponent(); ~CMyComponen ...
- [Tkey] OSU!
更新的题解可看 此处 你说得对但是 恐怖日本病毒会自动向你的电脑中下载 OSU! 题意简述 一个 01 串,每个位置有 \(p_{i}\) 的概率为 \(1\),连续的 \(x\) 个 \(1\) 贡 ...
- Kubernetes Deployment控制器(二十)
前面我们学习了 ReplicaSet 控制器,了解到该控制器是用来维护集群中运行的 Pod 数量的,但是往往在实际操作的时候,我们反而不会去直接使用 RS,而是会使用更上层的控制器,比如我们今天要学习 ...
- 《Vue.js 设计与实现》读书笔记 - 第5章、非原始值的响应式方案
第5章.非原始值的响应式方案 5.1 理解 Proxy 和 Reflect Proxy Proxy 只能代理对象,不能代理非对象原始值,比如字符串. Proxy 会拦截对对象的基本语义,并重新定义对象 ...
- 将ASD光谱仪的.asd文件转为文本文件
本文介绍基于ViewSpec Pro软件,将ASD地物光谱仪获取到的.asd格式文件,批量转换为通用的.txt文本格式文件的方法. ASD光谱仪是英国Malvern Panalytical公司 ...
- vue3读取所有的vue文件
- vue3中没有 this 环境变量了
因为 api setup 在 beforecreate 之前执行,所以 this 是 undefined : setup 不能是一个 async 函数 ,因为返回值不是 对象了 ,而是 promise ...