美文网首页
vue网络请求笔记

vue网络请求笔记

作者: GoodBoy0208 | 来源:发表于2020-05-10 14:50 被阅读0次

    接口格式(举例)

    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数据
          })
        }
      }
    }
    

    相关文章

      网友评论

          本文标题:vue网络请求笔记

          本文链接:https://www.haomeiwen.com/subject/nrjjnhtx.html