Jquery 当通过AJAX请求调用方法/路由时,重定向不起作用
我在Jquery 当通过AJAX请求调用方法/路由时,重定向不起作用,jquery,ajax,symfony,routing,Jquery,Ajax,Symfony,Routing,我在listDevice.html.twig模板上有这个代码 $.ajax({ type: "GET", url: "http://dev/app_dev.php/device/" + window.id + "/remove", crossDomain: false, dataType: "html", error: function(data) { console.log("Error in Processing-----" + dat
listDevice.html.twig
模板上有这个代码
$.ajax({
type: "GET",
url: "http://dev/app_dev.php/device/" + window.id + "/remove",
crossDomain: false,
dataType: "html",
error: function(data) {
console.log("Error in Processing-----" + data.status);
}
});
当有人点击delete
按钮时会触发该代码(无所谓)。这是在DeleteController.php
处理删除的函数:
/**
* @Route("/device/{id}/remove", name="device_remove")
* @param integer $id
* @param Request $request
* @Method({"GET"})
* @Template()
* @Security("has_role('ROLE_ADMIN')")
* @return array
*/
public function removeAction(Request $request, $id = null)
{
$em = $this->getDoctrine()->getManager();
$can_delete = $em->getRepository('DeviceBundle:UnassignedDevices')->findBy(array('id' => $id));
if (count($can_delete) == 0) {
$driverHasDevice = $em->getRepository("DeviceBundle:DriverHasDevice")->findOneBy(array('device' => $id));
$deviceToRemove = $em->getRepository("DeviceBundle:Device")->findOneBy(array('id' => $id));
if ($driverHasDevice) {
$em->remove($driverHasDevice);
}
$em->remove($deviceToRemove);
$em->flush();
$flashMessage = $this->get('translator')->trans('delete.success', array('%element%' => 'device'));
}
else {
$flashMessage = $this->get('translator')->trans('devices.messages.driver.assigned');
}
$this->get('session')->getFlashBag()->add('message', $flashMessage);
return $this->redirect($this->generateUrl('device_list'));
}
两者都很好,我的意思是当我点击
delete
按钮时,路由匹配,代码被执行,但是重定向,这一行,$this->redirect($this->generateUrl('device_list')代码>永远不会发生,为什么?如何解决这个问题?您可能正在向ajax调用返回一个原始html字符串。我做了一些调查。我相信你需要这样做:
header("Location:" + $this->generateUrl('device_list')); /* Redirect browser */
exit();
有关更详细的说明,请参见或
$.ajax({
type: "GET",
url: "http://dev/app_dev.php/device/" + window.id + "/remove",
crossDomain: false,
dataType: "html",
complete: function (data) { // add this option and see what "data" is
console.log(data);
},
error: function(data) {
console.log("Error in Processing-----" + data.status);
}
})) this->redirect
所做的基本上是返回带有302
http状态的响应,即redirect。这不会被ajax调用解析(它等待200ok
status),所以不能这样做
您想要实现的另一个解决方案是返回常规(json
,在我的示例中)响应,其中包含您要重定向到的url,并通过设置window.location.href
在success
回调中执行此操作:
JS:
您的控制器:
use Symfony\Component\HttpFoundation\JsonResponse;
public function removeAction(Request $request, $id = null)
{
(...)
$this->get('session')->getFlashBag()->add('message', $flashMessage);
return new JsonResponse($this->generateUrl('device_list'));
}
你能像我刚才添加的代码那样添加完整选项并查看返回的数据吗?
console.log
什么也不输出,我只得到完整的HTML代码OK,这是你的原始php或我发布的php?你能发布你得到的吗。你说它什么都不记录,但是你把html拿回来了,让我们看看。
use Symfony\Component\HttpFoundation\JsonResponse;
public function removeAction(Request $request, $id = null)
{
(...)
$this->get('session')->getFlashBag()->add('message', $flashMessage);
return new JsonResponse($this->generateUrl('device_list'));
}