0

I'm sending a POST request using AJAX like so:

var time = Date.now();
request = $.ajax({
    data : 'time=' + time,
    url : '/php/save.php',
    type : 'POST',
    success : function(response) {
        alert(response);
    }
});

The save.php file looks as follows:

<?php
header('Content-type: text/plain');
header('Access-Control-Allow-Origin: *');
echo $_POST["time"];
?>

And the error message I am getting is:

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

According to questions like this one, the headers I've added should have done the trick. But also, the error itself is a bit confusing because clearly the save.php is in the same domain as the index.html file.

I would be very grateful for any suggestions. Apart from the PHP file configuration I've shown above I've tried many others as suggested by similar questions or answers on Google - no luck so far.

Thanks!

Community
  • 1
  • 1
tsotsi
  • 551
  • 2
  • 7
  • 17

1 Answers1

-1

This is happening because your hosting doesn't allow the domain to use the POST method in request. You unfortunately can use the GET method... if allowed:

request = $.ajax({
    /* ... */
    type : 'GET',
    /* ... */
});

Note: the data you send go to the URL parameters, then in the server-side you should index $_GET, but not $_POST to get data values.

Otherwise you'll need to migrate from this hosting...

Klaider
  • 3,565
  • 3
  • 23
  • 52