Chris_F Chris_F - 1 year ago 85
Ajax Question

Can a form (loaded from a page with AJAX) be submitted using AJAX?

I am working on a userscript for an internal website (witten ASP.NET WebForms, I believe). I am trying to automate some things and what I would like to accomplish is load a page via AJAX and then submit a form on this page using AJAX. The form has multiple submit buttons and I want to perform the equivalent action of clicking one of these buttons. Below is a simplified version of what I am using at the moment. It doesn't seem to work (the action that would have been performed if I had loaded the page and clicked the button normally did not occur) and I'm not sure if I've perhaps made a mistake.

fetch(URL, {
credentials: 'same-origin'
}).then(response => response.text()).then(text => {
const parser = new DOMParser();
const doc = parser.parseFromString(text, 'text/html');
const form = doc.getElementById('Form1');
const button = doc.getElementById('uc_btnDelete');
const data = new FormData(form);
data.set(, button.value); //submit the form as if uc_btnDelete was clicked
fetch(form.action, {
method: form.method,
body: data,
credentials: 'same-origin'
}).then(response => response.text()).then(text => {

Answer Source

To answer my own question it appears that the issue was with the referring URL not being what the server was expecting. I think that window.history.replaceState() is my solution to this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download