views:

49

answers:

3

Hi, 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 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!

+1  A: 

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

Liam Bailey
DELETE and PUT are valid HTTP request methods, See point 9.6 and 9.7: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
PatrikAkerstrand
There is no Delete, or Put in jQuery.ajax()
Liam Bailey
Fair enough. Seems I can't remove the down-vote unless the answer is edited?
PatrikAkerstrand
edited to clarify.
Liam Bailey
I was wrong. THere is, just not supported in all browsers. http://api.jquery.com/jQuery.ajax/
Liam Bailey
Well, I am unsure about the chrome supporting the request, but with the output firefox is showing in the console, I feel firefox supports the delete request. But the question is why is it responding with 301 response then jumping to GET request? I am editing my question to add some info about .htaccess file on my server.
Goje87
A: 

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?

Slavic
Hey Slavic, Thanks for the idea :) But I need to use the DELETE request as I am implementing the REST services. I am not sure if the problem is with the browser, server or the .htaccess file. Could you suggest me any steps with which the problem can be narrowed down to server alone.
Goje87
You should try this on a local server (locahost) with no "safe" features: use chrome or mozilla since these 2 have the most chances of allowing jQuery to use PUT and DELETE. Then just output your $_SERVER['REQUEST_METHOD']; in a php script. Should be elementary as this.
Slavic
A: 

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.

Goje87