1

I've been doing some searching and trying to figure out how to build a request to send through something like

   $.ajax({
            contentType: "multipart/form-data",
            type: "POST",
            data: data,
            url: url,
            success: function (data) { }
        });

Now the request itself should look something like this where it's a json job part, then json detail part, and associated file

Content-Type: multipart/form-data; boundary=4fed9b4a-7aae-430f-b907-cbefe7e22c55
--4fed9b4a-7aae-430f-b907-cbefe7e22c55
Content-Type: application/vnd.collection+json
Content-Disposition: form-data; name=job

{json}

--4fed9b4a-7aae-430f-b907-cbefe7e22c55
Content-Type: application/vnd.collection+json
Content-Disposition: form-data; name=details

{json}

--4fed9b4a-7aae-430f-b907-cbefe7e22c55
Content-Type: application/pdf
Content-Disposition: form-data; filename=moo.pdf

xxxxxxxxxxxxxxxxx
--4fed9b4a-7aae-430f-b907-cbefe7e22c55
Content-Type: application/vnd.collection+json
Content-Disposition: form-data; name=details

{json}

--4fed9b4a-7aae-430f-b907-cbefe7e22c55
Content-Type: application/pdf
Content-Disposition: form-data; filename=doh.pdf

xxxxxxxxxxxxxxxxxxxxxxxxxxx
--4fed9b4a-7aae-430f-b907-cbefe7e22c55--

So using Javscript and assuming i have the file data and the json data how would i construct a request that i could send in the above ajax call? Thanks

Philip Loyer
  • 738
  • 2
  • 7
  • 22
  • I'm not sure you are going to be able to do it with jquery. If it were me I might spend some looking at this: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest – dudeman Mar 13 '15 at 18:25
  • I will explore that option as well, thanks! – Philip Loyer Mar 13 '15 at 18:52

1 Answers1

0

Try adding contentType: false and processData: false to your ajax request. The answer to this SE question includes a good explanation of what's going on, basically you're forcing jQuery to not include the content-type header.

Community
  • 1
  • 1
scurrie
  • 403
  • 1
  • 6
  • 12