zoukankan      html  css  js  c++  java
  • formData使用append追加key/value后console为空的问题(已解决)

    在上传图片的时候需要对选择的图片通过表单的形式提交给后台,如下

    handleEditorImgAdd(pos , $file){
                console.log(pos,$file)
                // 创建一个FormData空对象,然后使用append方法添加key/value
                var formdata = new FormData();
                formdata.append('image',$file);
                console.log(formdata)
                
                this.$http.post('/article/uploadImg', formdata).then(res =>{
                    //将返回来的url替换到原本的位置
    
                })
            },

    这样打印出的formdata为空,查看formadata的API才知道是需要调用它的方法才能获取到。

    formdata接口将键值对格式的数据以表单的方式提交给后台。

    属性不是直接挂载在到FormData实例上。我们可以通过它提供的迭代器,或者get方法去取值。

    解决方案:FormData.get("键名")

     

    handleEditorImgAdd(pos , $file){
                console.log(pos,$file)
                // 创建一个FormData空对象,然后使用append方法添加key/value
                var formdata = new FormData();
                formdata.append('image',$file);
                console.log(formdata.get('image'))
                
                this.$http.post('/article/uploadImg', formdata).then(res =>{
                    //将返回来的url替换到原本的位置
    
                })
            },

    这样就可以了。

    不积跬步无以至千里
  • 相关阅读:
    Strust2学习笔记
    EL与OGNL区别
    十进制与其他进制转换
    JSTL
    <jsp:include>和<%@include%>区别
    AngularJS 内置过滤器
    ubuntu下swift安装
    ubuntu下gerrit 安装部署
    java日期操作
    SpringMVC GET请求中文数据传递到Server端乱码
  • 原文地址:https://www.cnblogs.com/lyt0207/p/13278404.html
Copyright © 2011-2022 走看看