ajax fromdata传值,jQuery的AJAX“的multipart / form-data的”不发送数据?

I'm at a loss for why I can't get jQuery to pass upload data seeing as the AJAX object appears to be configured correctly, and the correct Content-Type/MIME-Type headers are being sent.

I've tried two separate forms of request--one with a FormData object contained within a literal, and also just passing the FormData object directly.

Unfortunately either way I can't get anything to pass, and both $_FILES and $_POST are empty arrays.

The ideal request I wish to use is as follows:

VxMTE.png

Along with the following code:

var files = new FormData();

$.each(context.prototype.fileData, function(i, obj) { files.append(i, obj.value.files[0]); });

var request = { action: 'upload', id: response.obj.id, data: files };

$.ajax({

type : 'POST',

url : context.controller,

data : request,

processData : false,

contentType : 'multipart/form-data',

mimeType : 'multipart/form-data',

success : function(r) {

console.log(r);

//if (errors != null) { } else context.close();

},

error : function(r) { alert('jQuery Error'); }

});

Once again the only response (looking at both the Network tab & Console) when I try to export both $_FILES and $_POST is simply two empty arrays...

解决方案

You have to pass the FormData object as the data parameter

var request = new FormData();

$.each(context.prototype.fileData, function(i, obj) { request.append(i, obj.value.files[0]); });

request.append('action', 'upload');

request.append('id', response.obj.id);

$.ajax({

type : 'POST',

url : context.controller,

data : request,

processData : false,

contentType : false,

success : function(r) {

console.log(r);

//if (errors != null) { } else context.close();

},

error : function(r) { alert('jQuery Error'); }

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值