如何防止Ext JS使用restful商店在DELETE请求中包含实体主体?

时间:2010-06-25 15:17:00

标签: rest extjs

当Ext JS从restful商店发出DELETE请求时,它包含一个实体主体。虽然这个doesn't seem to be forbidden符合HTTP规范,但Google App Engine不接受此类请求。所以我想知道是否有办法阻止restful商店在DELETE请求中包含冗余实体主体。

详细信息:

使用此样本作为参考: http://www.sencha.com/deploy/dev/examples/restful/restful.html

这就是商店的定义方式:

var store = new Ext.data.Store({
    id: 'user',
    restful: true,     // <-- This Store is RESTful
    proxy: proxy,
    reader: reader,
    writer: writer
});

按下“删除”按钮后,这是Ext JS发送的请求:

DELETE http://www.sencha.com/deploy/dev/examples/restful/app.php/users/6 HTTP/1.1
Host: www.sencha.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; pt-BR; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pt-br,pt;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.sencha.com/deploy/dev/examples/restful/restful.html
Content-Length: 10
Cookie: bb_sessionhash=8d75f5e42d576fb695a02bf1d24c9ff1; etc...

{"data":6}

当此格式的请求(包含“数据”内容)提交给Google App Engine时,它会回复:

400 Bad Request

2 个答案:

答案 0 :(得分:7)

您可以通过覆盖HttpProxy类中的方法来解决此问题。首先,添加以下代码:

// Special HttpProxy that sends no body on DELETE requests
Ext.data.GAEHttpProxy = Ext.extend(Ext.data.HttpProxy, {
doRequest: function(action, rs, params, reader, cb, scope, arg) {
    if(this.api[action]['method'].toLowerCase() == "delete") {
        delete params.jsonData;
    }

    Ext.data.GAEHttpProxy.superclass.doRequest.call(this, action, rs, params, reader, cb, scope, arg);
}
});

然后,在代码的其余部分中使用这个新类(“GAEHttpProxy”)而不是HttpProxy(例如,当您创建在上面显示的商店中使用的代理时)。这对我有用,我希望它适合你!

答案 1 :(得分:0)

虽然问题在7年前被问到,我们现在有了问题6,但问题还没有解决OOTB。所以这是我的工作解决方案:

Ext.define('My.Proxy', {
    extend: 'Ext.data.proxy.Rest',
    writer: {
        type: 'json',
        writeAllFields: true, // may be false, as you wish
        transform: {
            fn: function(data, request) {
                return request.config.action === 'destroy' ? null : data;
            },
            scope: this
        }
    }
});

我们也可以执行此检查:request.config.method === 'DELETE'但由于某种原因,它始终返回false。所以我建议留在action === 'destroy'