Extjs 5 ajax PUT和DELETE方法抛出403错误(包括csrf令牌)

时间:2015-05-28 16:11:09

标签: ajax django rest extjs5 django-csrf

我正在使用django-rest-framework和extjs5构建一个Web应用程序。 显然我遇到了django的csrf令牌问题,我不得不在Extjs的Ajax请求中加入。 但是,虽然我成功实现了POST方法,但我的实现似乎并不适用于PUT和DELETE方法。

我的POST方法代码:

 onSaveRecordBtnClick: function(){
    Job_Name = this.lookupReference('Job_Name').getValue();     

    var csrf = Ext.util.Cookies.get('csrftoken');
    Ext.Ajax.request({
        url: '/jobs_api/job/',
        method: "POST",
        params: {
            Job_Name: Job_Name,
            'csrfmiddlewaretoken': csrf
        },
        success: function(conn, response, options, eOpts) {
            var result = MyApp.util.Util.decodeJSON(conn.responseText);
            if (result.success) {
                alert('Job Submission Successfull');
            }
            else {
                MyApp.util.Util.showErrorMsg(conn.responseText);
            }
        },
        failure: function(conn, response, options, eOpts) {
            MyApp.util.Util.showErrorMsg(conn.responseText);
        }
    });
}

这很有效,但是当我尝试PUT或DELETE方法时,我会一直得到:
请求方法:DELETE
状态代码:403 FORBIDDEN
{"详细信息":" CSRF失败:CSRF令牌丢失或不正确。"}

我的DELETE方法:

onJobDblClick : function(grid, record, index, eOpts) {
    var job_id = record.id;
    var csrf = Ext.util.Cookies.get('csrftoken');
    Ext.Ajax.request({
         url: '/jobs_api/job/' + job_id + '/',
         method: "DELETE",
         params: {
            'id': job_id,
            'csrfmiddlewaretoken': csrf
         },
         success: function(conn, response, options, eOpts) {
            var result = MyApp.util.Util.decodeJSON(conn.responseText);
            if (result.success) {
                alert('Job Deleted Successfully');
            }
            else {
                MyApp.util.Util.showErrorMsg(conn.responseText);
            }
        },
        failure: function(conn, response, options, eOpts) {
            MyApp.util.Util.showErrorMsg(conn.responseText);
        }
    });
}

我的工作模式是:

Ext.define('MyApp.model.Job', {
extend: 'MyApp.model.Base',
fields: [
    { name: 'id', type: 'int' },
    { name: 'Job_Name', type: 'string' },
],
proxy: {
    type: 'rest',
    url: '/jobs_api/job/',
    reader: {
        type: 'json',
        rootProperty: 'data'
    }
}
});

我不知道为什么会这样。请帮忙!!

0 个答案:

没有答案