php - 如何向 Symfony 添加 Ajax 功能

标签 php jquery ajax symfony

我在一个页面中有一组 session ,我想使用 AJAX 将其删除。即点击链接,无需导航到新页面,只需删除 session ,并显示成功消息。

现在,根据给定的答案(对我仍然不起作用),我有以下内容:

Controller

use Symfony\Component\HttpFoundation\JsonResponse;
//..

public function ajaxRemoveSessionAction()
{
    $session = $this->getRequest()->getSession();
    $session->remove('name');

    return new JsonResponse(array('success' => true));
}

路由:

ajax_remove_session:
    pattern:  /remove-session
    defaults: { _controller: FooTestBundle:Page:ajaxRemoveSession }

Twig :

<a href="#" id="remove_session">Remove session</a>

<script type="text/javascript">
$(document).ready(function() {
    $('#remove_session').click(function(){
        event.preventDefault();

        $.ajax({
          url: {{ url('ajax_remove_session') }},
          cache: false,
          success: function(result){
             $(".success").append(result);
          }
        });
    });
});
</script>

更新问题:

在路由、 Controller 和模板中找到所有代码


Controller: PageController.php

/src/Simon/TestBundle/Controller/PageController.php

<?php

namespace Simon\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\JsonResponse;


class PageController extends Controller
{
    public function helloAction($name)
    {


        $session = new Session();
        $session->start();

        $session->get('name', 'Drak');
        $session->get('name');

        $session->getFlashBag()->add('notice', 'Profile Updated');

        $messages = null;

        foreach($session->getFlashBag()->get('notice', array()) as $message){
            $messages = $message;
        }


        return $this->render('SimonTestBundle:Page:index.html.twig', array('name' => $name.' '.$messages));
    }


    public function ajaxRemoveSessionAction()
    {
        // Destroy the desired session
        $session = $this->getRequest()->getSession();
        $session->remove('name');

        return new JsonResponse(array('success' => true));
    }
}

Template: Twig template

/src/Simon/TestBundle/Resources/views/Page/index.html.twig

{% extends 'SimonTestBundle::layout.html.twig' %}

{% block body %}
    <a href="#" id="remove_session">Remove session</a>



    <script type="text/javascript">
        $('#remove_session').click(function(e){
            e.preventDefault();

                $.ajax({
                    url: {{ url('ajax_remove_session') }},
                    cache: false,
                    success: function(html){
                        // do something on success
                    }
                }).fail(function(event){
                            console.log(event);
                        });
            });
        });
    </script>



{% endblock %}

Routing:

/src/Simon/TestBundle/Resources/config/routing.yml

simon_test_homepage:
    pattern:  /hello/{name}
    defaults: { _controller: SimonTestBundle:Page:hello }

ajax_remove_session:
    pattern:  /remove-session
    defaults: { _controller: SimonTestBundle:Page:ajaxRemoveSession }

最佳答案

示例 Controller :

use Symfony\Component\HttpFoundation\JsonResponse;


public function ajaxRemoveSessionAction()
{
    // Destroy the desired session
    $session = $this->getRequest()->getSession();
    $session->remove('name');

    return new JsonResponse(array('success' => true));
}

路由示例:

ajax_remove_session:
    pattern:  /remove-session
    defaults: { _controller: FooTestBundle:Page:ajaxRemoveSession }

示例 Twig :

<a href="#" id="remove_session">Remove session</a>

<script type="text/javascript">
$(document).ready(function() {
    $('#remove_session').click(function(){
        event.preventDefault();

        $.ajax({
          url: {{ url('ajax_remove_session') }},
          cache: false,
          success: function(html){
            // do something on success
          }
        });
    });
});
</script>

这些只是需要测试的例子。

关于php - 如何向 Symfony 添加 Ajax 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19355562/

相关文章:

javascript - 如何在 jQuery Mobile 中设置 "list item > link"的样式

javascript - 提交表单时如何发送JSON请求参数? (而不是 $.ajax 请求)

java - JSF、AJAX、使用 selectOneMenu 更新动态表

javascript - while 循环因 jquery 中的某些数学函数而崩溃

jquery - 显示 : inline-block doesn't work on ajax response

javascript - 为什么没有数据消息显示? (试图创建谷歌图表)

php - 如何在 smarty codeigniter 中获取基本 url

php - 使用正则表达式获取文本开头的 # 个字符

php - C2DM 和 GCM 有什么区别

jQuery Modal + .show() 和 .hide()