本文介绍了jQuery的AJAX“的multipart / form-data的”不发送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为什么我不能因为AJAX对象似乎是正确配置的jQuery通过上传数据看到的损失,正确的内容类型/ MIME类型的头被发送。

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.

我试过两个独立的申请表 - 之一,包含在字面上一个FORMDATA对象,也刚好路过FORMDATA对象直接

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.

不幸的是无论哪种方式,我不能得到任何通过,既$ _FILES和$ _ POST是空的数组。

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:

随着下面的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'); }

});

再一次唯一的反应(看着两个网络选项卡和放大器;控制台),当我尝试导出两个$ _FILES和$ _ POST简直就是两个空数组...

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...

推荐答案

您必须将 FORMDATA 对象传递的数据参数

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'); }

});

这篇关于jQuery的AJAX“的multipart / form-data的”不发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:54