1

I have an jQuery AJAX call like so:

var ajaxStuff = $.ajax({
    type : 'POST',
    url : customURL,
    data : {
        myData : 'myData'
    },
    dataType : 'json',
    async : false
}).responseText;
console.log(ajaxStuff);

However, in the PHP when I ask what request method I'm using:

echo ($_SERVER["REQUEST_METHOD"]);

It returns:

GET

Why can't my AJAX call be recognized as a POST?

Thanks!

  • 3
    Does the URL at `customURL` do any redirects? – Jonnix Feb 27 '18 at 16:26
  • Unrelated to your question, but it is highly advised that you do not do `async : false` – Patrick Q Feb 27 '18 at 16:27
  • `async:false` is almost never the solution -> [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Andreas Feb 27 '18 at 16:28

1 Answers1

0

The parameter name of the method is method, not type:

var ajaxStuff = $.ajax({
    method: 'POST',  ////// not "type"
    url : customURL,
    data : {
        myData : 'myData'
    },
    dataType : 'json',
    async : false
}).responseText;
console.log(ajaxStuff);

Docs: http://api.jquery.com/jquery.ajax/

$.ajax() defaults to GET when no method is specified, or when the parameter is named incorrectly.

WillardSolutions
  • 2,316
  • 4
  • 28
  • 38