XMLHttpRequest using multipart/form-data

N

neil-holmquist

i have this code:

var parameters = "temp1=1"
http_request.onreadystatechange = saveHatHandler;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "multipart/form-data");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);

where http_request is a valid XMLHttpRequest object.

my 'url' is a php file that has this for now:

<?php
$retval = phpversion();
$retval .= "<br>";
$retval .= print_r($_POST);
echo $retval;
?>

my handler just issues an alert with the result of
http_request.responseText

if i switch the Content-type to "application/x-www-form-urlencoded" it
works fine and the result i see is:

Array
(
[temp1]=> 1
)
4.4.2<br>1

if i switch the Content-type to "multipart/form-data" i see this:

Array
(

)
4.4.2<br>1

I eventually want to use this method for sending a file name to upload
to a DB so i need to use POST.

any ideas why this isn't working..i've looked all over for examples and
nothing i'm doing seems out of the ordinary..it just isn't working.

any help would be greatly appreciated..cause i'm about to pull out my
hair..haha

Thanks,
-neil.
 
T

Thomas 'PointedEars' Lahn

[...]
var parameters = "temp1=1"
http_request.onreadystatechange = saveHatHandler;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "multipart/form-data");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
[...]
<?php
$retval = phpversion();
$retval .= "<br>";
$retval .= print_r($_POST);
echo $retval;
?>
[...]
if i switch the Content-type to "application/x-www-form-urlencoded" it
works fine and the result i see is:

Array
(
[temp1]=> 1
)
4.4.2<br>1

Works as designed.
if i switch the Content-type to "multipart/form-data" i see this:

Array
(

)
4.4.2<br>1
[...]
any ideas why this isn't working..

It does work with application/x-www-form-urlencoded only because the data
you include in the message body your POST request ("temp1=1") is _not_
multipart/form-data, but application/x-www-form-urlencoded data.
I eventually want to use this method for sending a file name to upload
to a DB so i need to use POST.

multipart/form-data is the correct media type for submitting files via HTTP.
But you will have to conform to the specified _multipart_ format to make it
work as designed:

<URL:http://www.rfc-editor.org/rfc/rfc2388.txt>

See also <URL:http://www.iana.org/assignments/media-types/>. (Note that
application/x-www-form-urlencoded is not a registered media type, but only
best common practice.)


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top