Ajax 为什么Symfony控制器返回模板使用<;html>;及<;车身>;标签?
我有一个控制器动作:Ajax 为什么Symfony控制器返回模板使用<;html>;及<;车身>;标签?,ajax,symfony,twig,symfony4,Ajax,Symfony,Twig,Symfony4,我有一个控制器动作: /** * Get the template for the notifications in the user-navbar * * @Route("/notification/get-template", name="get_notifications_template") * @param Request $request * @return Response */ public function getNotificationsTemplateActi
/**
* Get the template for the notifications in the user-navbar
*
* @Route("/notification/get-template", name="get_notifications_template")
* @param Request $request
* @return Response
*/
public function getNotificationsTemplateAction(Request $request)
{
if (!$request->isXmlHttpRequest()) {
return $this->redirect($this->generateUrl('homepage'));
}
return $this->render('Menu/_notifications_block.html.twig');
}
我想使用进行AJAX调用以获取此模板:
refreshNotifications() {
$.ajax({
url: this.$wrapper.data('refresh-action-path'),
method: 'POST',
}).then(function (data) {
// remove old notifications
this.$wrapper.remove();
// append data to the container
$('#user-notifications-container').append(data);
console.log(data);
}.bind(this))
}
现在的问题是-Symfony容器发送整个html页面:
模板是:
{% if app.user is not null and is_granted('ROLE_USER') %}
{% if app.user|notifications|length != 0 %}
{% set notifications = app.user|notifications %}
{% set all_notification_count = notifications|length %}
<li class="nav-item mx-2 dropdown js-notification-wrapper data-refresh-action-path="{{ path('get_notifications_template') }}"">
<a href="#" class="icon-wrapper nav-link btn-lg" data-toggle="dropdown">
<span class="icon icon-mail" aria-hidden="true"></span>
<span class="badge badge-notification">{{ all_notification_count }}</span>
</a>
<ul class="dropdown-menu dropdown-menu-right notification-list">
{% if app.user is not null and is_granted('ROLE_USER') and all_notification_count != 0 %}
{% for notification in notifications %}
<div class="notification-text">
<div class="d-flex">
<div class="col-10 px-0">
Kurszugang: {{ notification.courseAction.course }} ({{ notification.courseAction.course.school }})
- {{ notification.courseAction.schoolUser }}
</div>
<div class="col-2">
<button class="btn btn-sm btn-success" id="js-accept-course-action" data-course-action-path="{{ path('course_action_accept_join_request', {'courseAction' : notification.courseAction.id}) }}">
<i class="icon icon-thumbs-up"></i>
</button>
<button class="btn btn-sm btn-secondary" id="js-decline-course-action" data-course-action-path="{{ path('course_action_decline_join_request', {'courseAction' : notification.courseAction.id}) }}">
<i class="icon icon-thumbs-down"></i>
</button>
</div>
</div>
</div>
</li>
{% endfor %}
{% endif %}
</ul>
</li>
{% endif %}
{% endif %}
{%如果app.user不为null并且已被授予('ROLE\u user')%}
{%if-app.user |通知|长度!=0%}
{%set notifications=app.user | notifications%}
{%set all_notification_count=notifications | length%}
{%如果app.user不为null且已被授予(“角色用户”)且所有通知计数!=0%}
{%用于通知中的通知%}
Kurszugang:{{notification.courseAction.course}}({{notification.courseAction.courses.school})
-{{notification.courseAction.schoolUser}
{%endfor%}
{%endif%}
{%endif%}
{%endif%}
有人能告诉我-为什么我不能只剪下一个html页面,而是整个html页面
我无法将整个页面附加到容器中…按照我的理解,
render
返回完整的响应(带标题),而renderView
只返回html
尝试更改此行:
return $this->render('Menu/_notifications_block.html.twig');
为此:
return new JsonResponse([
'html'=> $this->renderView('Menu/_notifications_block.html.twig')
]);
$(data.html).appendTo('#user-notifications-container');
然后在ajax函数中更改:
$('#user-notifications-container').append(data);
为此:
return new JsonResponse([
'html'=> $this->renderView('Menu/_notifications_block.html.twig')
]);
$(data.html).appendTo('#user-notifications-container');
以防万一,如果您的ajax请求没有设置正确的头(请参阅:),或者您只是在浏览器中访问路由,那么该请求在默认情况下不是xmlHttpRequest,因此您将被重定向到主页。因此,请确保您发送的请求符合要求。或者-或者-删除整个if块,因为我看不到保留它的好理由。另一个注释:
$this->redirect($this->generateUrl('routename'))
可以替换为$this->redirectToRoute('routename')
。您提供了整个模板吗?似乎此模板正在扩展基础模板。