古い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
を使用してページをリフレッシュすると、その行は削除されたため消えてしまいます。これは正しい動作でなければなりません。
質問:私の解決策は何ですか? 2番目のAJAX呼び出しが行われたときにコントローラかjQueryコードからページを強制的に更新する方法はありますか?