Submitting data to a ReST asset using FormData


(Gavin) #1

Hi.

I am trying to submit form data to a squiz rest asset using the following javascript:

var formData = new FormData(document.getElementById(‘form-id’));

var http = new XMLHttpRequest();
var url = ‘’;
http.open(‘POST’, url, true);
http.setRequestHeader(‘Content-type’, ‘application/x-www-form-urlencoded’);

http.onreadystatechange = function() {

}
http.send(formData);

In the rest asset request body I have:

{
“title”: “n/a”,
“forenames”: “%globals_post_firstNames%”,
“surname”: “%globals_post_lastName%”
}

When I run this JS and check what the rest asset has sent onward I can see that the forenames and surnames have not been populated.
However if I submit the form normally (not using javascript) the data is populated.

Is there something funny about using JS FormData with Squiz ? Am I doing it right ? I can see the data being sent in Chrome devtools.

Thanks for any help,
Gavin.


(Bart Banda) #2

Probably need more info on what your implementation is to be able to advise. As long as your JS is submitting the exact same data as your non-JS solution, it should be fine, so it sounds like there is some missing data needed to be added by your JS?


(Gavin) #3

Thanks for the response Bart.
Its working ok now.
I was using JS FormData but setting the content-type to application/x-www-form-urlencoded
FormData posts everything using multipart/form-data
So its no wonder Squiz was getting confused.
I just removed the line setting the content-type and let it sort itself out.

Cheers,
Gavin.


(Bart Banda) #4

Great, good to hear it got sorted.