PUT请求从Backbone.js向我的RESTful PHP页面返回406(不可接受)


PUT request returns 406 (Not acceptable) coming from Backbone.js to my REST-ful PHP page

我在PHP中使用Fat Free Framework编写了一个REST-ful API,并使用backbone.js进行调用。当我尝试保存新的订单模型时,我的应用程序会发出PUT请求,服务器会返回406错误。

Request Method:PUT
Status Code:406 Not Acceptable
Request Headers
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:174
Content-Type:application/json
Cookie:__utma=239804689.76636928.1286699220.1305666110.1325104376.94; __utmz=239804689.1325104376.94.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); PHPSESSID=935d2632fd0d12a1a0df4cb0f392eb5e
X-Requested-With:XMLHttpRequest
Request Payload
{"id":0,"customerId":0,"lastPage":"items","priceConfig":null,"items":null,"saveStatus":0,"savedAt":1326588395899,"name":null}
Response Headers
Connection:Keep-Alive
Content-Length:460
Content-Type:text/html; charset=iso-8859-1
Date:Sun, 15 Jan 2012 00:46:37 GMT
Keep-Alive:timeout=5, max=98
Server:Apache

我的.htaccess文件如下:

# Enable rewrite engine and route requests to framework
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
# Disable ETags
<IfModule mod_headers.c>
    Header Unset ETag
    FileETag none
</IfModule>
# Default expires header if none specified (stay in browser cache for 7 days)
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault A604800
</IfModule>
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>

我的网站应用程序在我的本地服务器上运行良好,并且只在我的网络服务器上运行。有什么问题吗?

我想出了一个变通办法。

我相信我的服务器正在使用mod_security2来阻止PUT和DELETE请求。我正在等待他们的回复,并且不能在.htaccess文件中禁用mod_security2,所以我无能为力。

在.htaccess文件中使用"Script PUT/filename"导致500错误:"此处不允许使用脚本",我不确定原因,但我决定不处理将web主机重新配置为处理PUT和DELETE的问题。

为了保持API的REST-ful,我保留了PUT和DELETE的正常处理,并将其添加到POST处理中:

function post() {
    //if Backbone.emulateHTTP is true, emulate PUT
    $data = json_decode(F3::get('REQBODY'), true);
    $type = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']; //PUT, DELETE, POST
    if ($type == 'PUT') {
        $this->put();
        return;
    }
    if ($type == 'DELETE') {
        $this->delete();
        return;
    }
    //handle normal POST here
}

如果将Backbone.mulateHTTP=true;它将请求方法保持为POST,并将X-HTTP-method-Override发送为PUT或DELETE。

我喜欢这样,因为我可以保持RESTful实现的完整性,并且在发布到Web服务器时只需注释掉emulateHTTP代码。