0

How to find out which fetching API does a DOM component like HTML form uses while making http requests?

Edit: Is it possible to modify it from using one instead of another?

yokus
  • 145
  • 2
  • 10
  • If you're eligible to use DevTools you can find it in Fetch/XHR tab in Network section look at [@mplungjan](https://stackoverflow.com/a/76670619/18482482) response – Simone Baptiste Jul 12 '23 at 12:47

2 Answers2

3

HTML Forms are using the http protocol to send and receive data and not XHR or Fetch (which are however also using that protocol)

You cannot modify the form submission to use fetch or xmlhttprequest instead of the default protocol.

If you you, as the owner/developer of the web page do not want to use the default functionality of a form, then you can hook the submit event and use preventDefault to stop the submission and use your own implementation using for example fetch or XHR.

You do not even NEED a form to use fetch or xmlhttprequest, just gather the data using form elements and an optional button

If it is not your page, you can look in the console in the network tab to see what is used to send data to the server:

enter image description here

Normal post (testing using POST and a form tester URL)

enter image description here

Example:

NOTE1: StackOverflow does not allow writing to embedded iframes. To see the script below use the iframe, see this fiddle

NOTE2: The whole idea of using fetch or xmlhttprequest will fail if the server in the action of the form does not support CORS!

window.addEventListener("DOMContentLoaded", () => { // when page is loaded
  const form = document.getElementById("myForm");
  form.addEventListener("submit", (e) => {
    e.preventDefault(); // stop submission
    const tgt = e.target; // the form
    console.log(tgt.action)
    fetch(tgt.action, // the URL
        {
          "method": tgt.method,
          body: new FormData(tgt) // the content of the form
        }
      )
      .then(response => response.text()) // return the text
      .then(text => {
        console.log(text);
        try {
          const targetFrameDocument = document.getElementById(tgt.target).contentWindow.document;
          targetFrameDocument.write(`<pre>${text}</pre>`); // the pre is not needed if the site returns html
          targetFrameDocument.close();
        } catch (err) {
          const msg = err.message;
          console.log(msg.includes("cross-origin") ? "Sorry this site does not support iframe writing, see the fiddle instead" : msg);
        }
      });
  });
});
<form id="myForm" action="https://httpbin.org/post" method="POST" target="iframe1">
  <input type="text" name="field1" value="" placeholder="Type something and hit enter" />
  <input type="submit" name="anyNameBUTSubmit" value="Optional Submit button" />
</form>

<iframe name="iframe1" id="iframe1" src="about:blank"></iframe>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • I want form to use an abstraction layer. I could not see how to specify that in RFCs. Can you provide any official docs or tutorial on how to `choose between several methods, like fetch or XHR,`? – yokus Jul 12 '23 at 13:05
  • [Fetch](https://fetch.spec.whatwg.org/) / [XMLHttpRequest](https://xhr.spec.whatwg.org/) / [Difference](https://stackoverflow.com/questions/35549547/fetch-api-vs-xmlhttprequest) – mplungjan Jul 12 '23 at 13:08
  • [Abstraction](https://www.google.com/search?q=abstraction+layer+fetch+or+xmlhttprequest+site:stackoverflow.com) – mplungjan Jul 12 '23 at 13:09
  • I know these APIs but I am interested in is whether or not we can modify
    component somehow to use one or another? Not by preventing its default behaviour with something like e.prevent().
    – yokus Jul 12 '23 at 13:13
  • Comment from @Quentin combined with your answer completed the overall image . Thank you for your time and answers! – yokus Jul 12 '23 at 13:16
  • `If you hook the submit event you can use preventDefault to stop the submission and use your own implementation` – mplungjan Jul 12 '23 at 13:38
  • It is an unkown concept for me. However, if you believe modification by hooking, I am deleting my comment. Maybe an official doc on hooking would help me and people. Thank you. – yokus Jul 12 '23 at 14:11
  • https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener – mplungjan Jul 12 '23 at 16:07
  • I added a fetch example – mplungjan Jul 12 '23 at 16:49
  • I really appreciate the example you provided and your effort. However, in your example we ignore the form behaviour (let us say I want to display response in a iframe using target property of the form element) and just do fetch api stuff completely unrelated to main purpose of form element. – yokus Jul 12 '23 at 17:23
  • It was examples. I do not have time to write a version that goes to a real server, gets real HTML etc. The content is not unrelated at all. See the response, it contains what you entered in the form and what the form test URL responds. Of course you can change every aspect of what I wrote to suit your needs! See the update which is using an iFrame to write the response – mplungjan Jul 13 '23 at 05:54
1

How to find out which fetching API does a DOM component like HMTL form uses while making http requests?

It doesn't use either.

fetch and XMLHttpRequest are APIs the browser exposes to client-side JavaScript for making HTTP requests.

A <form> is handled internally by the browser, how that is done is a browser-specific implementation detail. I'm not aware of any browsers that use XHR or fetch as part of that implementation.

Edit: Is it possible to modify it from using one instead of another?

No. You can listen for a submit event then prevent the default behaviour before making your own HTTP request with fetch or XHR, but you can't change how forms are handled internally.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thank you, now I understand that form default behaviour is implemented behind the scenes and we can not modify it (only prevent it). + Behind the scenes implementation is a seperate context from the APIs given to the client of the browser. Thanks you for your time. – yokus Jul 12 '23 at 13:15