普通网友
2017-05-16 13:03
浏览 42
已采纳

使用AJAX和Symfony 3.x传递post参数时出错

I want to use Ajax to past POST parameters in one of my controller.

For this, i did this code :

<script type="text/javascript">
document.getElementById('navbar').style.visibility='hidden';

idacc = $("#idAcc").data();
console.log(idacc);
var form = $("#form");
div = form.find("div");
console.log(div);
$("#ajax").click(function(div){
        $.ajax({
            type: "POST",
            url: "/message/add/" + {{ id }},
            data: div,
            success: function(div){
                console.log(div);
                console.log($(this));
            }
        });
    console.log("click");
});
</script>

It's a little script that i've made while i was searching in the web.

My html struct is :

<div class="form>
    <div>...</div>
    <div>...</div>
    <div>...</div>
    <div>...</div>
    .....
</div>

So with my script, the div variable receive all of the div in the div with the "form" class. It's ok when i dump it in the success function, i have what i want. But when I see in the symfony toolbar POST parameters, there's nothing, i tried to do this as well :

data: { 'div': div }

I'm getting an error

Uncaught TypeError: Cannot read property 'defaultView' of undefined

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dsj83686 2017-05-16 13:16
    最佳回答

    If used twig construction {{ id }}, you must sure what your Controller-Action return 'id' => 'some id'.

    I recommend for you use {{ path() }}

    url: '{{ path("app.admin.search.certificate", {"cartId": cart_id}) }}',
    app.admin.search.certificate - path to you route.
    cartId - parameters for route and action.
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题