普通网友 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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办