javascript - FormData object always empty


I am trying to create form data to send within a post request (to a PHP script) with FormData.

However, the FormData object is always empty:

var oReq = new XMLHttpRequest();
var url = "";"POST", url, true);
var myFormData = new FormData();
myFormData.append("pdf", pdf.output(),"thisPDF.pdf");

the 'pdf' variable is a jsPDF object (I've checked that it is a valid object and I have tried to remove that line and only have the "test" form data element sent. Yet, myFormData is always an empty object. Hoping I'm just missing something simple here.

In addition, when I try to check for the existence of the form elements in the PHP script, it shows no $_POST and no $_FILE elements at all.


When I try this code:

var oReq = new XMLHttpRequest();
var url = "";"POST", url, true);
var myFormData = new FormData();

I can then access the "formType" $_POST variable in the php script. But if I add in the File append statement to the myFormData object (and keep the same 'Content-Type', the PHP script errors returning a "not allowed access" error.

However, the $_POST is set according to isset($_POST) in both cases and with the file inserted the $_FILE variable is set according to isset($_FILE).




Remove theoReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); from the code. Browser automatically set multipart/form-data header, if you add file in FormData.

People are also looking for solutions to the problem: php - Cannot log in to the MySQL server error


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.