DELETE request in ajax giving 301 moved response

I am trying to make a delete request using the jquery ajax method as follows

$.ajax(
{
  type: 'DELETE',
  url: '/tagz',
  data: {id: taskId},
  success: function(data)
  {
    console.log(data);
  }
});

And when I see the console in chrome, I find in the request headers that instead of a DELETE request, a GET request is created. and in the firefox console I see the following.

23:50:52:658: Network: DELETE http://test.goje87.com/tagz [HTTP/1.1 301 Moved Permanently 947ms]
23:50:53:614: Network: GET http://test.goje87.com/tagz/ [HTTP/1.1 200 OK 400ms]

On the server side, I just support the following code.

$reqMethod = $_SERVER['REQUEST_METHOD'];

switch($reqMethod)
{
  case 'GET':
    Utils::printR('Will provide the resource.');
    selectObjects();
    break;
  case 'POST':
    Utils::printR('Will create a new record.');
    createObject();
    break;
  case 'PUT':
    Utils::printR('Will update the record.');
    break;
  case 'DELETE':
    Utils::printR('Will delete the record.');
    Utils::output($_SERVER);
    break;
}

I do not see the request coming in case 'DELETE'. He gets in case 'GET'.

Below is the .htaccess file that I use on the server to clean up the urls.

RewriteEngine on
RewriteRule ^(.*)$ index.php [L,QSA]

Please help me make DELETE queries. Thank!

+3
source share
3 answers

I understood.:)

url ('/tagz'), $.ajax. /tagz/ ( ), Chrome Firefox.

, .

+2

DELETE, , Chrome.

+1

It appears that the web server does not allow the use of the DELETE method. Why don't you just use the POST method for all your requests and instead replace the variable β€œaction” that will contain the action that needs to be moved?

0
source

Source: https://habr.com/ru/post/1767778/


All Articles