重定向时强制刷新页面
Force page refresh on redirect
我正在为旧的 Zend Framework 1.10 项目修复一些错误,但在重定向和页面刷新方面遇到了一些问题。
问题:拨打AJAX电话,检查此人是否已分配保险,如果已分配,则不允许删除此人,直到它没有任何保险。
解决方法:
控制器:crud/application/controllers/personController.php
class PersonController extends Zend_Controller_Action
{
// this will fetch all the persons from DB and send to the view
public function indexAction()
{
$persons = new Application_Model_DbTable_Person();
$this->view->persons = $persons->fetchAll();
}
// this will check whether the person has or not insurances
public function hasinsurancesAction()
{
$hasInsurances = new Application_Model_DbTable_Person();
return $this->_helper->json(
['count' => count($hasInsurances->personHasInsurances($this->_getParam('id')))]
);
}
...
// this will delete the person from DB and will make a redirection to indexAction
public function deleteAction()
{
if ($this->getRequest()->isPost()) {
$person_id = (int) $this->getRequest()->getPost('id');
$person = new Application_Model_DbTable_Person();
$person->deletePerson($person_id);
$this->_helper->redirector('index');
}
}
}
观点:crud/application/views/scripts/company/index.phtml
<table>
<tr>
<th>Title</th>
<th> </th>
</tr>
<?php foreach ($this->persons as $person) : ?>
<tr>
<td><?php echo $this->escape($person->title); ?></td>
<td>
<a href="<?php echo $this->url(
[
'controller' => 'person',
'action' => 'edit',
'id' => $person->id,
]
); ?>">Edit</a>
<a class="delete"
data-id="<?php echo $person->id ?>"
data-href="<?php echo $this->url(
[
'controller' => 'person',
'action' => 'delete',
'id' => $person->id,
]
); ?>"
data-delete-href="<?php echo $this->url(
[
'controller' => 'person',
'action' => 'hasInsurances',
]
); ?>"
href="#">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
Javascript/jQuery:crud/public/js/delete.js
$(function () {
$('.delete').on('click', function () {
id = $(this).data('id');
href_attr = $(this).data('href');
delete_attr = $(this).data('delete-href');
$.ajax({
url: delete_attr,
data: {'id': id},
success: function (result) {
if (result.count > 0) {
alert('You can not delete this person. Try deleting associated insurances first.')
} else {
$.ajax({
url: href_attr,
data: {'id': id},
type: 'POST'
});
}
}
});
})
});
问题: 上面的代码工作正常但有一个差距,当调用 deleteAction()
并且人员被删除时,它会尝试重定向到 indexAction()
在那里我仍然看到被删除的人,我不应该。一旦我使用 F5
或 CTRL+R
刷新页面,该行就会消失,因为它已被删除,这应该是正确的行为。
问题:我的解决方案有什么问题?当第二次 AJAX 调用时,有什么方法可以从控制器或 jQuery 代码强制刷新页面?
如果你 delete_attr
是调用 PersonController->deleteAction()
的 URL 你的问题是你的浏览器做了一个 Ajax 请求(被重定向)但是返回的数据返回到您的 ajax 调用 (success: function (result) {
) 并且用户当前正在查看的页面是相同的。
你可以做的是添加一个新变量 (do_redirect => true
) 和你想将用户重定向到的 url,如果你的成功函数得到那个 url - 它应该做重定向:
class PersonController extends Zend_Controller_Action {
...
// this will delete the person from DB and will make a redirection to indexAction
public function deleteAction()
{
if ($this->getRequest()->isPost()) {
...
$person->deletePerson($person_id);
return $this->_helper->json(
['do_redirect' => TRUE, 'redirect_url' => 'index.php']
);
}
}
}
并且在你的 ajax 调用中你应该检查 do_redirect:
$.ajax({
url: delete_attr,
data: {'id': id},
success: function (result) {
if ('do_redirect' in result && 'redirect_url' in result) {
window.location.href = result.redirect_url
}
if (result.count > 0) {
alert('You can not delete this person. Try deleting associated insurances first.')
} else {
$.ajax({
url: href_attr,
data: {'id': id},
type: 'POST',
success: function(result) {
if ('do_redirect' in result && 'redirect_url' in result) {
window.location.href = result.redirect_url
}
}
});
}
}
});
我正在为旧的 Zend Framework 1.10 项目修复一些错误,但在重定向和页面刷新方面遇到了一些问题。
问题:拨打AJAX电话,检查此人是否已分配保险,如果已分配,则不允许删除此人,直到它没有任何保险。
解决方法:
控制器:crud/application/controllers/personController.php
class PersonController extends Zend_Controller_Action
{
// this will fetch all the persons from DB and send to the view
public function indexAction()
{
$persons = new Application_Model_DbTable_Person();
$this->view->persons = $persons->fetchAll();
}
// this will check whether the person has or not insurances
public function hasinsurancesAction()
{
$hasInsurances = new Application_Model_DbTable_Person();
return $this->_helper->json(
['count' => count($hasInsurances->personHasInsurances($this->_getParam('id')))]
);
}
...
// this will delete the person from DB and will make a redirection to indexAction
public function deleteAction()
{
if ($this->getRequest()->isPost()) {
$person_id = (int) $this->getRequest()->getPost('id');
$person = new Application_Model_DbTable_Person();
$person->deletePerson($person_id);
$this->_helper->redirector('index');
}
}
}
观点:crud/application/views/scripts/company/index.phtml
<table>
<tr>
<th>Title</th>
<th> </th>
</tr>
<?php foreach ($this->persons as $person) : ?>
<tr>
<td><?php echo $this->escape($person->title); ?></td>
<td>
<a href="<?php echo $this->url(
[
'controller' => 'person',
'action' => 'edit',
'id' => $person->id,
]
); ?>">Edit</a>
<a class="delete"
data-id="<?php echo $person->id ?>"
data-href="<?php echo $this->url(
[
'controller' => 'person',
'action' => 'delete',
'id' => $person->id,
]
); ?>"
data-delete-href="<?php echo $this->url(
[
'controller' => 'person',
'action' => 'hasInsurances',
]
); ?>"
href="#">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
Javascript/jQuery:crud/public/js/delete.js
$(function () {
$('.delete').on('click', function () {
id = $(this).data('id');
href_attr = $(this).data('href');
delete_attr = $(this).data('delete-href');
$.ajax({
url: delete_attr,
data: {'id': id},
success: function (result) {
if (result.count > 0) {
alert('You can not delete this person. Try deleting associated insurances first.')
} else {
$.ajax({
url: href_attr,
data: {'id': id},
type: 'POST'
});
}
}
});
})
});
问题: 上面的代码工作正常但有一个差距,当调用 deleteAction()
并且人员被删除时,它会尝试重定向到 indexAction()
在那里我仍然看到被删除的人,我不应该。一旦我使用 F5
或 CTRL+R
刷新页面,该行就会消失,因为它已被删除,这应该是正确的行为。
问题:我的解决方案有什么问题?当第二次 AJAX 调用时,有什么方法可以从控制器或 jQuery 代码强制刷新页面?
如果你 delete_attr
是调用 PersonController->deleteAction()
的 URL 你的问题是你的浏览器做了一个 Ajax 请求(被重定向)但是返回的数据返回到您的 ajax 调用 (success: function (result) {
) 并且用户当前正在查看的页面是相同的。
你可以做的是添加一个新变量 (do_redirect => true
) 和你想将用户重定向到的 url,如果你的成功函数得到那个 url - 它应该做重定向:
class PersonController extends Zend_Controller_Action {
...
// this will delete the person from DB and will make a redirection to indexAction
public function deleteAction()
{
if ($this->getRequest()->isPost()) {
...
$person->deletePerson($person_id);
return $this->_helper->json(
['do_redirect' => TRUE, 'redirect_url' => 'index.php']
);
}
}
}
并且在你的 ajax 调用中你应该检查 do_redirect:
$.ajax({
url: delete_attr,
data: {'id': id},
success: function (result) {
if ('do_redirect' in result && 'redirect_url' in result) {
window.location.href = result.redirect_url
}
if (result.count > 0) {
alert('You can not delete this person. Try deleting associated insurances first.')
} else {
$.ajax({
url: href_attr,
data: {'id': id},
type: 'POST',
success: function(result) {
if ('do_redirect' in result && 'redirect_url' in result) {
window.location.href = result.redirect_url
}
}
});
}
}
});