|
From: neil-holmquist on 19 Feb 2006 16:13 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.
From: Thomas 'PointedEars' Lahn on 19 Feb 2006 16:39 neil-holmquist(a)hawaii.rr.com wrote: > [...] > 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
|
Pages: 1 Prev: Load external JS using Javascript... Next: AJAX and HTTPS |