• vue中使用axios简单的封装post请求,并使用返回的数据


    发现在学习vue的时候,不论你用的是哪种编程工具,是否使用打包和脚手架,都需要手工的多练习,只能说步步是坑.

    在使用的过程中一定要多按F12,多写console.log来看输出的值是什么,非常有助于排错和知道返回的是啥东西

    1、在vue的data中定义一个数组pingxuanren,用于存放从服务器端请求来的数据

    data:{
        pingxuanren:[],     //需要评分的人员信息  
        userinfo:[],        //用户自己的身份信息
        userxx:''
    },
    

    2、然后简单的封装一下axios的post请求,我也是从网上看了很多,抄了一下,自己并不会写。

    //封装一下axios的POST请求
    axiosPost:function(url,params){
        return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: 'post',
            data: params,
            transformRequest: [function(data) {
               let ret = ''
               for(let it in data) {
                  ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
               }
               return ret
            }],
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
       })
       .then(res=>{
           resolve(res.data);
       })
      });
    },

    上面的直接复制就能用,返回的数据存储在Promise对像中,只要知道数据返回了就行了.

    3、具体的使用中需要注意params的参数,一定要写.then,返回的数据就是res,直接用就可以了.

     1  //返回被评分人的信息列表
     2  getyunangong:function(){
     3     var yhm=localStorage.getItem('yhm');
     4      5     var url='test.ashx?method=yuangong';
     6     var params={yhm_:yhm};
     7     this.axiosPost(url,params)
     8     .then(res=>{
     9         this.pingxuanren=res;
    10     })
    11  },

    4、pingxueren数组中就已经有数组了,可以读取绑定到页面上了。

    再看看原来的写法,乱的一批,每有不同的请求都得重写一次。

    axios({
                                url: 'test.ashx?method=savedata',
                                method: 'post',
                                data: {
                                    title_: _title,
                                    fen_: mes,
                                    yhm:localStorage.getItem('yhm'),
                                    tofen:localStorage.getItem('tofen'),
                                },
                                transformRequest: [function(data) {
                                    let ret = ''
                                    for(let it in data) {
                                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                                    }
                                    return ret
                                }],
                                headers: {
                                    'Content-Type': 'application/x-www-form-urlencoded'
                                }
                            })
                            .then(function (response) {
                                var info=response.data;
                                //console.log(info);
                                if (info===401){
                                    alert("保存数据时出错了!");
                                }else{
                                    alert("保存成功,点击'确定'将跳转至主评分页面!");
                                    localStorage.removeItem('tofen');
                                    window.location.href="home.htm";
    
                                }

      

  • 相关阅读:
    VC++ 利用PDB和dump文件定位问题并进行调试
    MFC限制edit控件的字符输入长度
    VC++ 使用CreateProcess创建新进程
    正则表达式验证HTTP地址是否合法
    C++ _access和_waccess的使用方法
    最后一次谈 VirtualBox的安装方法
    解决/var/log下没有messages文件的问题?
    待续未完- 自己写后台内容管理程序的辅助内容
    php中的正则函数:正则匹配,正则替换,正则分割 所有的操作都不会影响原来的字符串.
    未完待续
  • 原文地址:https://www.cnblogs.com/wjbych/p/12658971.html
Copyright © 2020-2023  润新知