vue网络请求笔记
2020-05-10 本文已影响0人
GoodBoy0208
接口格式(举例)
http://localhost:8080/server?req={
"head":{
"cmd":"",
"server_id":"",
"server_user_id":"",
"user_id":"",
"session_id":"",
"version":""},
"content":{"stationName":""}
}
1.首先添加axios, npm install axios
2.在api.js中
import axios from 'axios'
var URL = 'http://ip:port/testrequest'
var Server = {
head: {
cmd: '',
server_id: '',
server_user_id: '',
user_id: '',
session_id: '',
version: ''
},
content: {}
}
function GET(cmd, content) {
Server.head.cmd = cmd
Server.content = content
const data = Object.assign({},{
req : JSON.stringify(Server)
})
return axios.get(URL,{
params: data
}).then((res) => {
return Promise.resolve(res.data)
})
}
// 获取document信息
export function get_document_info(content) {
const cmd = 'get_document_info'
return GET(cmd, content)
}
3.在test.vue中script下
import {get_document_info} from '@/api/api'
export default {
data () {
return {}
},
mounted(){
this.loadData()
},
methods: {
loadData(){
let obj = {
kind: 'news',
pageindex: 1,
pagesize: 15,
}
get_document_info(obj).then((req) => {
console.log(req) //请求到的json数据
})
}
}
}