0

How do I check, if a POST XHR request was successful, on a website, for a particular URL?

If the POST was successful, I then want to run some JavaScript.

I've seen ways to do this in jQuery, but I want to do this via vanilla JavaScript.

Reena Verma
  • 1,617
  • 2
  • 20
  • 47
  • You can refer here https://developers.google.com/web/updates/2015/03/introduction-to-fetch You can use ``fetch API`` which is vanilla JavaScript Or if you want purely in XHR then refer this https://www.w3schools.com/xml/xml_http.asp – Not A Bot Oct 09 '20 at 11:25
  • This sounds like something you would need a browser extension for. – Musa Oct 09 '20 at 20:02

1 Answers1

2

You can use the status code to check if the request was successful:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
        // successful
    }
}
xhr.open('GET', 'http://example.com', true);
xhr.send();

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status

https://stackoverflow.com/a/3038972/10551293

swift-lynx
  • 3,219
  • 3
  • 26
  • 45
  • thanks for this. I don't want to make a fetch/get request though. I simply want to see, after a page as loaded, if any particular API calls were successful. – Reena Verma Oct 09 '20 at 11:35
  • @ReenaVerma I updated my answer. Hopefully this solves your problem now. – swift-lynx Oct 09 '20 at 11:44