cakephpで簡単なブログを作成しています。
その中で投稿の削除処理をjQueryを使ってajax化をしています。
ボタンをクリックするときちんと投稿は削除されるのですが、かんじんのふわっとした処理が追加されません、
何度も何度もコードを確認しましたが、ミスがみつけられませんでした。
また、function(res)の意味がりかいできないのですが、こちらはどういったいみなのでしょうか?
引数なのでしょうか?
edit.ctp{php(jsを埋め込んでます。)}
1<script> 2 3$(function() { 4 $('a.delete').click(function(e) { 5 if (confirm('sure?')) { 6 $.post('/posts/delete/'+$(this).data('post-id'), {}, function(res) { 7 $('#post_'+ res.id).fadeOut(); 8 }, "json"); 9 } 10 return false; 11 }); 12}); 13 14 15</script>
cakephpのPostscontroller.php
1 public function delete($id) { 2 if ($this->request->is('get')) { 3 throw new MethodNotAllowedException(); 4 } 5 6 if ($this->request->is('ajax')) { 7 if ($this->Post->delete($id)) { 8 $this->autoRender = false; 9 $this->autoLayout = false; // この2つはajax処理をするときの決り文句 10 $response = array('id' => $id); // idをjson形式で返す 11 $this->header('Content-Type: application/json'); 12 echo json_encode($response); 13 exit(); 14 } 15 } 16 $this->redirect(array('action' => 'index')); 17 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/19 02:42
2022/01/19 02:58
2022/01/19 05:28
2022/01/19 07:08
2022/01/19 08:50
2022/01/19 09:37
2022/01/19 11:12