22

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:

enter image description here

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

2
  • 2
    Try changing contentType to false Commented Oct 11, 2012 at 3:25
  • No luck. jQuery just changes it to application/xml but still no data in either $_POST or $_FILES Commented Oct 11, 2012 at 3:30

1 Answer 1

28

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'); } }); 
Sign up to request clarification or add additional context in comments.

2 Comments

Interestingly this didn't work at first, but it did once I changed Content-Type to false...
@Musa @julian how to get context? context.prototype.fileData ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.