Using Javascript to build a form with POST. When the form is submitted using AJaX to a Tomcat jsp page (or a servlet), the jsp page isn't retrieving form data.request.getParameter() is instead returning null.Also getting the same result when attempting to POST the form data using 'await fetch(test.jsp)'
A test javascript function to POST data to a Tomcat jsp page:
function send() { xhr = new XMLHttpRequest(); const formData = new FormData(); formData.append("recId", "5"); xhr.addEventListener('load', handleReturn); xhr.open('POST', 'test.jsp', true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send(formData); // xhr.send("recId=5"); return xhr;}
But in the target script 'test.jsp' -String rId = request.getParameter("recId");is returning null. Expecting '5'
This is the payload reported by Dev Tools:------WebKitFormBoundarysRv6NCasQdzagUnsContent-Disposition: form-data; name: "recId"5------WebKitFormBoundarysRv6NCasQdzagUns-
Have also tried using a form with a field named recId, eg:formData = new FormData(this-is-a-form);xhr.send(formData);
Same result.String rId = request.getParameter("recId");returns null instead of '5'
Have tried all the hints I could find online, and the documentation I found suggests this should work.What am I missing?
However, this (not using a form) does work:
function send() { xhr = new XMLHttpRequest(); // const formData = new FormData(); // formData.append("recId", "5"); xhr.addEventListener('load', handleReturn); xhr.open('POST', 'test.jsp', true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // xhr.send(formData); xhr.send("recId=5"); return xhr;}
This is the payload reported by Dev Tools:recId=5