Is it possible to detect the HTTP request method (e.g. GET or POST) of a page from JavaScript? If so, how?
Is it possible to detect the HTTP request method (e.g. GET or POST) of a page from JavaScript? If so, how?
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>
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.
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
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