Naming and posting forms with FormData and XMLHTTPrequest
I'm doing an exercise with JavaScript, and I'm after a couple of hours still stuck. I have this HTML form:
<form method=POST name=transferform
action="/transfer.php">
<input name=user type=text value="">
<input name=credits type=text value="">
<input type=submit name=submission value="Send">
</form>
I want to invoke a JavaScript that posts this form (filled in with some values), using XMLHTTPrequest and FormData. I've come this far, and to me this seems correct but it doesn't seem to work:
var formdata = new FormData();
formdata.append('user', 'bob');
formdata.append('credits', '1');
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://someurl.com/transfer.php');
xhr.send(formdata);
Using this script will not work, it doesn't post the form. However, manually pressing 'Send' in the HTML page will post the form, and all is well. My suspicion is that this doesn't work because I haven't set the name of the form in my request(the HTML form is named "transferform"). I can't figure out how to name the FormData-object for t开发者_高级运维he request.
I'm giving you the script of out context (it's a part of a larger exercise involving a web application provided to me to play with), but I hope you can help me anyway :)
I've been using this as my reference.
new FormData()
is encoded differently than a form submit
.
The default is enctype="application/x-www-form-urlencoded"
.
Using new FormData()
means that you are using enctype="multipart/form-data"
.
精彩评论