加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何向Symfony添加Ajax功能

发布时间:2020-12-13 17:45:54 所属栏目:PHP教程 来源:网络整理
导读:我在页面中有一组会话,我想使用 AJAX删除它.即点击链接,无需浏览新页面,只需删除会话,并在成功时显示消息. 现在,按照给定的答案(对我来说仍然不起作用),我有以下内容: 调节器 use SymfonyComponentHttpFoundationJsonResponse;//..public function ajaxR
我在页面中有一组会话,我想使用 AJAX删除它.即点击链接,无需浏览新页面,只需删除会话,并在成功时显示消息.

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

调节器

use SymfonyComponentHttpFoundationJsonResponse;
//..

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 }

枝条:

<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: PageController.php

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

<?php

namespace SimonTestBundleController;

use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationSessionSession;
use SymfonyComponentHttpFoundationJsonResponse;


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') }},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 }

解决方法

尝试将URL的选项值放在引号之间:

url: '{{ url('ajax_remove_session') }}',

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读