This is html file that submit ajax by button click to PHP script(on IIS).
But PHP script received wrong formatted data (there are brackets added [] and no parameter ‘section’ transmitted
What can be wrong
It would be good to have solution both: in JQuery and pure javascript
——————- HTML
<!DOCTYPE html>
<html STYLE="height:100%;">
<head></head>
<body>
<SCRIPT>
function zPostToTopic_ajax(){
var url='http://the_server/infospace/php/infospace2.php';
var formData2 = new FormData();
formData2.append('section', 'general');
formData2.append('action2', 'preview');
http_request=new XMLHttpRequest();//work for IE11 too, // code for IE7+, Firefox, Chrome, Opera, Safari
http_request.open("POST", url);
//------------------------------------
http_request.onreadystatechange = function() {
if(http_request.readyState == 4 && http_request.status == 200)
alert(http_request.responseText)
}
http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http_request.send(formData2);
}
</SCRIPT>
<FORM NAME=form_post_to_topic ID=form_post_to_topic METHOD=POST action="http://the_server/infospace/php/infospace2.php">
  <INPUT TYPE=BUTTON VALUE=Send onClick="zPostToTopic_ajax();return false;">
</FORM>
</body>
</html>
————————– PHP script
<?php
print_r($_REQUEST);
?>
————————— Received data:
Array
(
[-----------------------------276402058428
Content-Disposition:_form-data;_name] => "section"
general
-----------------------------276402058428
Content-Disposition: form-data; name="action2"
preview
-----------------------------276402058428--
)
2
Answers
Use jQuery’s
.ajax()
function. Here’s an example where I post a file upload too.Your problem is that you are setting the wrong content type for your request. When you use a formdata object the content type will be multi-part/formdata.
So when you are using a formdata object you do not set the content type and it is set for you.