HTTP DELETE请求没有正文 [英] HTTP DELETE request without body

查看:214
本文介绍了HTTP DELETE请求没有正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临同样的问题,如



这是我的代码:

  Ext.define('TT.proxy.CustomRestProxy',{
别名:'proxy.customrestproxy',
extends:'Ext.data.proxy.Rest',

buildRequest:function(operation){

var request = this.callParent(arguments);

if(operation.action ==='destroy')
{
delete request.operation.records;
}
return request;
}
});

defineStore = function(storeName,modelName,url){
var storeProperties = {
extends:'Ext.data.Store',
require:modelName,
model:modelName,
id:storeName,

proxy:{
type:'customrestproxy',
url:url,
batchActions:
noCache:false,
标题:{'Content-Type':'application / json'},
reader:{
type:'json',
totalProperty:'total',
successProperty:'success',
root:'data'
},
writer:{
type:'json'
},
}
};

Ext.define(storeName,storeProperties);
};

我会接受解决这个问题的任何答案,它不必包括ExtJS特有的功能,也就是拦截AJAX请求或类似的技术。

解决方案

有一个基于AJAX拦截器的解决方法,灵感来自于此链接。这个代码解决了所使用的框架的问题,所以对于不一定使用Ext JS的其他人也是有用的:

 (function(XHR){
use strict;

var open = XHR.prototype.open;
var send = XHR.prototype.send;
var httpMethod;

XHR.prototype.open = function(method,url,async,user,pass){
httpMethod = method;
this._url = url;
open.call(this,method,url,async,user,pass);
};

XHR.prototype.send = function(data){
if (httpMethod ==='DELETE')
{
data = null;
}
send.call(this,data);
}
} )(XMLHttpRequest的);


I am facing the same problem as mentioned here: I am trying to connect my ExtJS 4.1 store with REST API, but when I delete the record from the store and consequently invoke HTTP DELETE method, it gets rejected by the server-side because the HTTP request that ExtJS sent contains body. Unfortunately, the accepted answer on the link above is not valid for version 4 of ExtJS and higher.

The best that I achieved so far is to send empty array (literally, [] ) as a body, but of course this is still rejected:

This is my code:

Ext.define('TT.proxy.CustomRestProxy', { 
    alias: 'proxy.customrestproxy', 
    extend: 'Ext.data.proxy.Rest', 

    buildRequest: function(operation) {

        var request = this.callParent(arguments);

        if(operation.action === 'destroy')
        {                                
            delete request.operation.records;
        }
        return request; 
    }
});

defineStore = function(storeName, modelName, url) {
    var storeProperties = {
        extend: 'Ext.data.Store',
        requires: modelName,
        model: modelName,
        id: storeName,

        proxy: {
            type: 'customrestproxy',
            url: url,
            batchActions: false,
            noCache: false,
            headers: { 'Content-Type': 'application/json' },                
            reader: {
                type : 'json',
                totalProperty: 'total',
                successProperty: 'success',
                root: 'data'
            },
            writer: {
                type : 'json'
            },              
        }
    };

    Ext.define(storeName, storeProperties);
};

I would accept any answer that solves this issue, it does not have to include ExtJS-specific features, i.e. intercepting AJAX request or similar technique is also welcome.

解决方案

There is a workaround based on AJAX interceptor, inspired by this link. This code solves the problem regardless of the framework used, so it can be also useful for other people who are not necessarily using Ext JS:

(function(XHR) {
    "use strict";

    var open = XHR.prototype.open;
    var send = XHR.prototype.send;
    var httpMethod;

    XHR.prototype.open = function(method, url, async, user, pass) {
        httpMethod = method;
        this._url = url;
        open.call(this, method, url, async, user, pass);
    };

    XHR.prototype.send = function(data) {
        if(httpMethod === 'DELETE')
        {
            data = null;            
        }        
        send.call(this, data);
    }
})(XMLHttpRequest);

这篇关于HTTP DELETE请求没有正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆