FormData-jquery-vue

  • 原文地址
  • 很棒的推荐

jqeury格式

$.ajax({
    url: '/upload',
    type: 'POST',
    cache: false,
    data: new FormData($('#uploadForm')[0]),
    processData: false,
    contentType: false
}).done(function(res) {
  console.log(res)
}).fail(function(error) {
  console.log(error)
});

标准格式

var formData = new FormData();
formData.append('file', $('#file')[0].files[0]);
$.ajax({
    url: '/upload',
    type: 'POST',
    cache: false,
    data: formData,
    processData: false,
    contentType: false
}).done(function(res) {
  console.log(res)
}).fail(function(error) {
  console.log(error)
});

vue使用formdata

 var config = {
      baseURL: 'http://api.xiehur.com/'
    };

    var fileData = new FormData();
    fileData.append("token", "ZHSy75asLZa4nwwBc8lJYBOy0U-oSTxD_1484494816");
    fileData.append("app_id", "xxxxxx-xxx-xxxxxxx");
    fileData.append("page", "1");
    fileData.append("page_size", "10");

    axios.post('wechat/grouplist', fileData,config)
        .then(function (response) {
            if (response.data.code == 0) {
                userData.people =response.data.items;
            }
        })
        .catch(function (error) {
            console.log(error);
        });


参数详解

你可能感兴趣的:(FormData-jquery-vue)