前提・実現したいこと
CakePHP2で、Ajaxを用いてデータの削除をさせたいと思っています。
dotinstallの動画を見て、コーディングしてみたのですが、
削除リンクをクリックすると、confirmは表示され、OKボタンをクリックすると、
POST http://localhost/management/profiles/delete/14 500 (Internal Server Error)
というエラーを発せられます。
色々調べてみたのですが、該当するような記事を見つけることができませんでした。
間違っている部分をご指摘頂けますと幸いです。
該当のソースコード
//index.ctp <tbody> <?php foreach ($users as $user) : ?> <tr id="post_<?php echo $user['Profile']['id']; ?>"> <td class="center"><?php echo $user['Profile']['employee_id']; ?></td> <td class="center"><?php echo $user['Profile']['name']; ?></td> <td class="center"><?php echo $user['JobHistory'][0]['JobCategory']['name']; ?></td> <td class="center"><?php echo $user['Profile']['created']; ?></td> <td class="center"><?php echo $user['Profile']['modified']; ?></td> <td class="center"> <a class="btn btn-success" href="#"> <i class="glyphicon glyphicon-zoom-in icon-white"></i> 表示 </a> <a class="btn btn-danger delete" href="#" data-post-id="<?php echo $user['Profile']['id']; ?>"> <i class="glyphicon glyphicon-trash icon-white"></i> 削除 </a> </td> </tr> <?php endforeach; ?> </tbody>
//index.ctp <script> $(function(){ $('a.delete').click(function(e){ if (confirm('本当に削除してもよろしいですか?')) { $.post('/management/profiles/delete/'+$(this).data('post-id'), {}, function(res){ $('#'+res.id).fadeOut(); }, 'json'); } return false; }); }); </script>
//ProfilesController.php public function delete($id) { //ここに飛んでこない。 if ($this->request->is('get')) { throw new MethodNotAllowedException(); } if ($this->request->is('ajax')) { if ($this->Profile->delete($id)) { $this->autoRender = false; $this->autoLayout = false; $response = array('id' => $id); $this->header('Content-Type: application/json'); echo json_encode($response); exit(); } } $this->redirect(array('action'=>'index')); }
その他に不足情報があれば、ご指摘ください。
何卒宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。