JQuery ajax DELETE请求在OPTIONS阶段失败



我试图用JQuery发送一个删除请求,但我一直收到一个500内部服务器错误。我用来发送请求的代码是:

$('#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;
});

尽管我在使用Postman(Chrome HTTP客户端扩展)时,以及在使用完全相同的AJAX函数发送get或POST时,都会得到200 OK的响应。只是PUT和DELETE不起作用。我已经在最新的Chrome和Firefox以及安卓手机上进行了测试,所以不认为浏览器有问题。有人能告诉我代码可能出了什么问题吗?

感谢

编辑

请求和响应标头:

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

控制器代码:

当我试图调试这个控制器时,它只是:

class ReviewController extends BaseController {
public function destroy($id)
{
dd('delete');
}
}

dd()是Laravel函数do or die。

这肯定是服务器问题。

我不知道您使用的是什么服务器,但以下是我如何处理节点中的OPTIONS:

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();
}
};

最新更新