English 中文(简体)
如何在DOJO工具箱中进行ajaxSetup beforeEnd之类的东西,以在DOJOAJAX POST中提供CSRF令牌
原标题:how to do ajaxSetup beforeSend like stuff in DOJO toolkit to provide CSRF token in DOJO AJAX POSTs

Django的最新版本要求csrf_token与每个POST请求一起发送(无论是通过AJAX还是通过普通请求)。

CSRF处理中的缺陷已修复

他们建议,Django现在将接受自定义HTTP标头X-CSRFTOKEN中的CSRF令牌,以及表单提交本身,以便与流行的JavaScript工具包一起使用,该工具包允许在所有AJAX请求中插入自定义标头。

他们在jQuery中给出了一个这样做的例子

$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
            // Only send the token to relative URLs i.e. locally.
            xhr.setRequestHeader("X-CSRFToken",
                                 $("#csrfmiddlewaretoken").val());
        }
    }
});

I am not able to figure out how to do something similar in DOJO toolkit. I have been using dojo.rpc.JsonService() for a while.

请提出一种在DOJO中做类似事情的方法?

或者唯一的选择是分别对每个xhrPost请求执行此操作?

问题回答

免责声明:我不熟悉django。

除了扩展基本的dojo-xhr定义(不推荐)之外,我想说您可能想要扩展dojo.xhr或构建一个实用程序方法。

实用程序方法将混合或设置传递到dojo.xhr的参数的“headers”属性:

myCustomNameSpace.xhr = function (xhrArgs) {
var csrfHeader = { "X-CSRFToken" : dojo.byId(csrfmiddlewaretoken).val(); }; xhrArgs.headers?dojo.mixin(xhrArgs.headers,csrfHeader):xhrArgs.headers=csrfHeader; dojo.xhrPost(xhrArgs);
};

通过myCustomNameSpace.xhr调用({method:“POST”,url:“http://www.....“});

{%csrf_token%}将以如下形式出现:

<input type= hidden  name= csrfmiddlewaretoken  value= ...  />

所以,只需将您的内容混合如下:

var form = dojo.byId("postForm");
dojo.xhrPost({url:form.action,
    content:{text:form.text.value, csrfmiddlewaretoken:form.csrfmiddlewaretoken.value},
)




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签