php – 如何向Symfony添加Ajax功能

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何向Symfony添加Ajax功能脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我在页面中有一组会话,我想使用 AJAX删除它.即点击链接,无需浏览新页面,只需删除会话,并在成功时显示消息.

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

调节

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 }

枝条:

<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>

更新问题:

使用路由,控制器和模板中的所有代码

/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));
    }
}

/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 %}

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何向Symfony添加Ajax功能全部内容,希望文章能够帮你解决php – 如何向Symfony添加Ajax功能所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。