0

I need to pass data from fancybox iframe to my django view using post. I set the iframe title as "a href" link that the user can click on.

this is the code for the fancybox:

    $(".fancybox-company").fancybox({
    width       : '75%',
    height      : '75%',
    autoSize    : false,
    closeClick  : false,
    fitToView   : false,
    openEffect  : 'none',
    closeEffect : 'none',
    type : 'iframe',
    beforeLoad: function() {
        var id = $(this.element).data('company-id');
        this.title = "<a href=\"\" id=company-iframe>Add to your list</a>";
    }
});

and this is the code that listens on click events:

$( ".company-iframe" ).on( 'click', function ( event ) {

    event.preventDefault();

    var id = event.target.href;

    $.post( "/company/selection", { id : id }, function ( json ) {

      // ...

    } );

this is not being called, i need to pass the click event from the iframe to the parent and pass the company id as input.

what am i missing?

10x

4

1 回答 1

0

我认为您缺少 csrf 令牌。这是一种django 机制,用于防止跨站点请求伪造。如果您在中间件类中启用了 csrf,而不是在每个发布请求中,您必须在数据中提供“csrfmiddlewaretoken”。您可以使用 {% csrf_token %} 标签从 cookie 或直接在模板中访问它。

在模板中实现此标记后,您需要修改 js 函数,例如

$( ".company-iframe" ).on( 'click', function ( event ) {

    event.preventDefault();

    var csrfmiddlewaretoken = $('input[name=\"csrfmiddlewaretoken\"]').val();
    var id = event.target.href;

    $.post( "/company/selection", 
             { id : id , csrfmiddlewaretoken: csrfmiddlewaretoken},
             function ( json ) {

      // ...

    } );
于 2012-12-04T16:10:55.290 回答