Question

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

Was it helpful?

Solution

In a word - No

OTHER TIPS

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 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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top