I've been struggling to find a solution for this issue. The closest I found was a thread on Stack Overflow about composing multipart/form-data with different Content-Types, but it didn't fully address my problem.
Here's the situation - I have a .NET webservice that requires a POST request with both JSON data (such as title and content) and files uploaded by the user. I'm sending this request from a DOJO Dialog box using an AJAX post request.
The challenge is to use 'multipart/form-data' while gathering data from multiple sources beyond just form elements. My code snippet looks something like this:
var values = { // created JS object with data from various sources };
var formData = new FormData();
formData.append('data', json.stringify(values));
var files = document.getElementById("files").files;
var file = files[0];
formData.append("file0", file);
xhr.post('link_to_my_service',
{
handleAs: "json",
sync: false,
data: formData,
headers: {
'X-Requested-With': '',
'Content-Type': false,
'Accept': 'application/javascript, application/json'
}
})
The request reaches the server successfully, however, the issue arises due to the lack of a 'Content-Type' set in the 'data' part of the request. This has proven difficult to resolve. One suggestion involved creating a BLOB and passing the JSON string through it, but this solution wasn't effective either.
I'm currently at a standstill and unsure how to proceed given that modifying the service isn’t an option. Any insights on how to work around this limitation within Dojo 1.10 would be greatly appreciated.