jQuery AJAX 'multipart/form-data' Not Sending Data?

天涯浪子 提交于 2019-12-27 22:12:36

问题


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:

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


回答1:


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

});


来源:https://stackoverflow.com/questions/12831680/jquery-ajax-multipart-form-data-not-sending-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!