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
2 Comments
url() references -and send these to your handler as well; see, possible.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>
1 Comment
<meta id="request-method" name="request-method" content="@Request.HttpMethod">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
1 Comment
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.
Comments
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