Sending XMLHttpRequest with FormData Sending XMLHttpRequest with FormData ajax ajax

Sending XMLHttpRequest with FormData


You're missing the Content-Type header to specify that your data is form-like encoded.

This will work:

var xhr = new XMLHttpRequest(),    data = "data%5Btumblelog%5D=drunknight&data%5Bsource%5D=FOLLOW_SOURCE_REBLOG";xhr.open('POST','https://www.somesite.com/page', false);// LINE ADDEDxhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");xhr.send(data);

EDIT

FormData is generally used to send binary data and will automatically set the Content-Type header to multipart/form-data (see FormData Spec and FormData Examples). However you have to make sure the server also accepts request using this MIME-type, which apparently is not your case, as you already tried and it didn't work.


Posting this as an answer because I believe it will give you exactly what you want to know;

I just want to know what sort of method is used to send that kind of data. I can assure you that the first image is obtained using an XHR

You haven't given enough information for us to see how the "correct" version is generated, but you can capture the relevant bits you want, drop in code like this before the working XMLHttpRequest is .opened;

(function (obj, methods) {    var i;    function log() {        console.log.apply(console, arguments);    }    for (i = 0; i < methods.length; ++i)        obj[methods[i]] = (function (method_name, method) {            return function () {                log.call(null, method_name, arguments);                return method.apply(this, arguments);            }        }(methods[i], obj[methods[i]]));}(XMLHttpRequest.prototype, ['open', 'send', 'setRequestHeader']));

Now perform the action to make it fire and the relevant parameters will be logged so you can see exactly what happens to it as it is set up and sent, which should allow you to know what to pass into your one.


The problem is that I get a forbidden (403)

I'm going to assume this is not a same origin security-error because this looks server-returned and not a browser-initiated abort