如何向Symfony添加Ajaxfunction
我在页面中有一组会话,我想使用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 }
枝条:
Remove session $(document).ready(function() { $('#remove_session').click(function(){ event.preventDefault(); $.ajax({ url: {{ url('ajax_remove_session') }}, cache: false, success: function(result){ $(".success").append(result); } }); }); });
更新问题:
使用路由,控制器和模板中的所有代码
控制器:PageController.php
/src/Simon/TestBundle/Controller/PageController.php
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 %} Remove session $('#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); }); }); }); {% 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') }}',
示例控制器:
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 }
示例枝条:
Remove session
这些只是需要测试的例子。
在你的AJAX调用中你使用的是全局event
对象,它不是跨浏览器的,不应该被使用(例如Firefox没有它)。
改为在函数调用中传递事件:
$('#remove_session').click(function(e){ e.preventDefault(); // rest of your code });
如果你这样做,jQuery会关注跨浏览器规范化和正确的行为。
另请参阅此问题以了解有关全局event
对象与传递到函数的事件之间差异的更多信息。
如果它仍然不起作用
- Chrome中的开放式开发者工具(F12)
- 如果在单击按钮后出现任何错误报告,请检查控制台( 控制台选项卡)
- 此外,打开网络选项卡,看看点击按钮后是否有任何请求; 什么是回应状态?
- 确保您的控制器使用GET请求(因为您没有指定请求类型,并且
GET
是默认的) - 您也可以尝试将
dataType: "json"
添加到您的AJAX请求中,但这不应该是一个问题,因为Symfony的JsonResponse
应该已经提供了必要的响应头
检查其他地方
- 你发布的错误说出了意想不到的地方
:
是; 调查那条线; 你在这里发布的代码是你网页上的whole
javascript吗?
在你对@Laurent Wartel的回答发表评论之后,我假设你现在得到了Uncaught SyntaxError: Unexpected token }
。 现在很清楚, click()
函数结束时还有其他括号。
此外,在绑定click()
事件之前,不要等待加载文档。 因此,jQuery尝试将事件绑定到尚未呈现的元素。 这是一个常见的陷阱。
要在页面加载后运行代码,您必须将其包装为info $(function() { ... }
。请阅读此处的完整说明,因为这是基本的,但非常重要。
总结一下,正确的代码如下:
看起来你在调试过程中已经删除了那一行,因为它存在于你的原始问题中,但是后来发送的源代码中没有。