Is it possible to detect the HTTP request method (e.g. GET or POST) of a page from JavaScript? If so, how?
7 Answers
In a word - No

- 1,295
- 2
- 13
- 9
-
well, there are various ways you can work around this specific case, and "NO" is incorrect. It is possible, depending on if you're willing to do the work involved, you basically have to create a DOM onload/mutation trigger, and a XHR wrapper -which triggers the same handler in both cases .. then for CSS, you can use your onload/mutation trigger to scan through CSS loaded for `url()` references -and send these to your handler as well; see, possible. – Apr 02 '19 at 22:30
-
Than the answers is still no, right? I can create a wrapper for everything and basically emit for whatever I want If I want to put the work in. But that's NOT the same as "detecting" a HTTP request. So I agree, the answer is no. But depending on the results you want to achieve there might be a work around – Y. Gherbi Jul 15 '22 at 02:54
I don't believe so. If you need this information, I suggest including a <meta>
element generated on the server that you can check with JavaScript.
For example, with PHP:
<meta id="request-method" name="request-method" content="<?php echo htmlentities($_SERVER['REQUEST_METHOD']); ?>">
<script type="text/javascript">
alert(document.getElementById("request-method").content);
</script>
You can check the page's referrer:
document.referrer == document.URL
If it's the same page it's quite likely that the user submitted the form.
Of course this requires
- that you don't link from a page to itself (which is required for accessibility anyway)
- that the form is submitted to the very same page it's on
- that the user did not disable the referrer

- 4,783
- 2
- 26
- 51
If you need this functionality, have the server detect what method was used and then modify something in the DOM that you can then read out later.

- 16,145
- 6
- 29
- 32
You cant do this for a normal post/get however you can get to this info if you use an xmlhttp call and use the getResponseHeader

- 78,161
- 20
- 151
- 159
No.
JS is a client-side programming language meaning everything is client side. You can use PHP to do this however or any server-side language.
Here is a example if you were to use php:
<?php
$foo = $_POST["fooRequest"]; # The actual response.
# do something with the foo variable like:
# echo "Response got: " + $foo;
?>
Then add some HTML:
<form action="test.php" method="post">
<input type="text" class="foo" name="fooRequest" placeholder="Testing requests" />
<button type="submit" name="submitButton">Send</button>
</form>
The above code sends a POST request then in the PHP we get the 'fooRequest'. However if you were to use JS, that's not possible like I said its a client side programming langauge
I know there is already a answer but i just wanted to explain a little more.
Try this
function getURIQueryString(){
var params = {};
var qstring = window.location.toString().substring(window.location.toString().indexOf("?") + 1);
var regex = /([^&=]+)=([^&=]+)/g;
var m;
while (m = regex.exec(qstring)){
params[decodeURIComponent(m[1])] = decodeURIComponent(m[2])
}
return params
}
It usually works. For example to get a get parameters named test. Use this
getURIQueryString().test
But It is impossible to get a post request

- 55
- 7