【问题标题】:Symfony delete ajaxSymfony 删除 ajax
【发布时间】:2015-05-05 15:06:09
【问题描述】:

当我想在模板 Twig 中使用 Ajax 和 Symfony 删除一个寄存器时,我遇到了问题。

<tbody>
    {% for entity in entities %}
        <tr>
            <td>
                <a class="delete btn btn-danger btn-xs glyphicon glyphicon-trash" data-playgroup-id="{{ entity.id }}" ></a>
            </td>
        </tr>
    {% endfor %}
</tbody>

阿贾克斯:

$(document).ready(function() {

    $(".delete").click(function(){
        var pid = $(this).attr("data-playgroup-id");

        bootbox.confirm("Are you sure?", function(result) {
            if(result){
                $.ajax({ 
                    url: '{{path('playergroup_delete', { 'id': pid}) }}',
                    type: 'delete', 
                    success: function(result) {
                        console.log('Delete');
                    },
                    error: function(e){
                        console.log(e.responseText);
                    }
                });
            }
        });
    });
});

我收到下一个错误:

变量“pid”不存在。

谢谢!

【问题讨论】:

  • 您的 url 属性看起来很时髦。查看报价,以及输出应该是什么...var url = "{{path('playergroup_delete', { 'id': " + pid + "}) }}"
  • @PhilCooper 它没有,它是 twig 语法
  • @jean 我的立场是正确的!
  • @PhilCooper 并非总是如此。这不是一个好习惯,但您可以编写内联 javascript 并使用 Twig 在其中输出变量或路由。

标签: php jquery ajax symfony twig


【解决方案1】:

正如 MouradK 所说,您在 twig 函数(服务器端)中传递了一个变量,并且您正在使用 javascript(客户端)获取该变量。 要解决这个问题,请执行以下操作:

$(document).ready(function() {

    $(".delete").click(function(){
        var pid = $(this).attr("data-playgroup-id");

        bootbox.confirm("Are you sure?", function(result) {
            url = '{{path('playergroup_delete', { 'id': 0}) }}';
            url = $url.replace("0",pid);
            if(result){
                $.ajax({ 
                    url: url,
                    type: 'delete', 
                    success: function(result) {
                        console.log('Delete');
                    },
                    error: function(e){
                        console.log(e.responseText);
                    }
                });
            }
        });
    });
});

【讨论】:

  • 关于这样的事情
  • 是的,找到了!怎么能告诉'type'属性Ajax它可以被删除也可以得到。作为 routing.yml 。 pg_delete:路径:/{id}/delete 默认值:{ _controller: "MyAppBundle" } 要求:{ _method: get|delete } 例如:类型:'get|delete';
  • 为什么删除后不重定向到分配的url?在删除记录的控制器函数上,返回:playergroup_delete: path: /{id}/delete defaults: { _controller: "MyAppBundle:PlayerGroup:delete" } requirements: { _method: get|delete }控制器:return $this-&gt;redirect($this-&gt;generateUrl('playergroup'));
  • 发现:success: function(result) { location.href = '{{path('playergroup')}}'; }
【解决方案2】:

这意味着您没有将 pid 变量传递给您的 Twig 模板。

通过控制器传递它,你会没事的

【讨论】:

  • $pid = $em->getRespository('MyAppBundle:PlayerGroup')->find();但在我的 indexAction 中,我不按 id 搜索,因为我只找到所有寄存器。
【解决方案3】:

错误是:

您正尝试在您的 twig 模板(服务器端)中设置来自客户端 (javascript) 的变量。

【讨论】:

  • 我可以在控制器中定义变量并将其留空吗?
  • 这取决于你的路由配置,如果它有一个默认值。
猜你喜欢
  • 2020-01-24
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多