4

I am trying to send a delete request with JQuery but I keep getting a 500 internal server error. The code I am using to send the request is:

$('#deleteReview').click(function(event, ui){
    var id = $('#editReviewId').val();

    $.ajax({
        type: 'DELETE',
        url: 'http://api.domain.com/v1/reviews/' + id,
        data: 'delete=1' + accessTokens,
        dataType: 'json',
        success: function(data) {
            ...
        },
        error: function(data) {
            ...
        }
    });
    return false;
});

Although I get a 200 OK response when using Postman (Chrome HTTP client extension) and also when I send a GET or POST using the exact same AJAX function. It is just PUT and DELETE that do not work. I have tested in the latest Chrome and Firefox as well as on an Android phone so do not think the browser is the problem. Could anyone guide me towards what could be wrong with the code?

Thanks

EDIT

The request and response headers:

Request URL:http://api.domain.com/v1/reviews/11
Request Method:OPTIONS
Status Code:500 Internal Server Error


Request Headers
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Access-Control-Request-Headers:accept
Access-Control-Request-Method:DELETE
Connection:keep-alive
Host:api.domain.com
Origin:http://client.domain.dev
Referer:http://client.domain.dev/v4/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.68 Safari/537.36


**Response Headers**
Access-Control-Allow-Methods:*
Access-Control-Allow-Origin:*
Connection:close
Content-Type:text/html
Date:Sun, 12 Jan 2014 01:34:28 GMT
Server:Apache
Set-Cookie:laravel_session=4dd2062c4e6837c605c7b0b7e21d417c; expires=Sun, 12-Jan-2014 03:34:28 GMT; path=/; HttpOnly
Transfer-Encoding:chunked

Controller Code:

As I am trying to debug this the controller is simply:

    class ReviewController extends \BaseController {

    public function destroy($id)
{
    dd('delete');
}
    }

dd() being the Laravel function do or die.

  • possible duplicate of [How to send a PUT/DELETE request in jQuery?](http://stackoverflow.com/questions/2153917/how-to-send-a-put-delete-request-in-jquery) – SOReader Jan 12 '14 at 01:17

1 Answers1

3

It's definitely a server issues.

I don't know what server you are using, but here's how I handle OPTIONS in node:

var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, api_token, user_token');
    if ('OPTIONS' == req.method) {
        res.send(200);
    } else {
        next();
    }
};
Brian Noah
  • 2,962
  • 18
  • 27
  • 1
    Thanks, I ended up adding `if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { header("HTTP/1.1 200 OK"); die(); }` and got it working –  Jan 12 '14 at 10:21
  • 1
    Both Access-Control-Allow-Methods as well as a 2xx status code are necessary for this to work. Found out the hard way when my server returned a 500 even though the headers were good. – tommybananas Apr 03 '14 at 20:55