Absolutely! The process involves retrieving the form from the document body, creating a FormData object with field values, and sending a POST request.
To simplify things, I utilized the Fetch API. You can learn more about it here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API. Additionally, I assumed that credentials are required to be sent from the same origin.
fetch('<GET URL>', {method: "GET", credentials: 'same-origin'})
.then((response) => (response.text()))
.then((responseBody) => {
var html = parseHTML(responseBody);
var form = html.querySelector('#my-form'); // Assuming this is the form ID
var formData = new FormData();
formData.append('someName', 'someValue'); // Field name should correspond to form fields
postForm(formData)
.then((response) => (response.text()))
.then((responseBody) => {
// Handle response from form submission here
});
})
function parseHTML(string) {
var doc = new DOMParser;
return doc.parseFromString(string, 'text/html').body;
}
function postForm(formData) {
return fetch('<POST URL>', {
method: 'POST',
body: formData,
credentials: 'same-origin'
})
}