开发者

DELETE request in ajax giving 301 moved response

开发者 https://www.devze.com 2023-01-19 01:09 出处:网络
I am trying to make a delete request using ajax method of jquery as follows $.ajax( { type: \'DELETE\', url: \'/tagz\',

I am trying to make a delete request using ajax method of jquery 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 a GET request is made 开发者_JAVA技巧instead of DELETE. and in 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 server side I am simply maintaining 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 don't see the request getting into the case 'DELETE'. It's getting into the case 'GET' instead.

Following is the .htaccess file that I am using at the server for the purpose of clean urls.

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

Please help me in making DELETE requests. Thanks!


I got it. :)

The problem was with the url ('/tagz') that I was passing in $.ajax. When I changed it to /tagz/ (added another forward slash at the end) it started working fine in Chrome and Firefox.

Thanks guys.


The DELETE method is obviously not supported by the Chrome browser.


It looks like the web server does not allow for DELETE method. Why don't you just use POST method for all your requests and supply instead a variable "action" that will contain the action to be carried?

0

精彩评论

暂无评论...
验证码 换一张
取 消