【问题标题】:CakePHP Ajax deleteCakePHP Ajax 删除
【发布时间】:2014-10-28 08:21:25
【问题描述】:

我正在尝试通过 ajax 删除 cakephp 数据。这里我已将 postlink 按钮更改为简单的 html 按钮,如

<button class="del" id=<?php echo $user['User']['id']; ?>>Delete </delete>

现在我可以通过下面的代码获取用户 ID

$('document').ready(function(){
        $('.del').click(function(){
            var x=$(this).attr("id");
            alert(x);
        });
    });

我已成功获取用户的 id。现在我正在尝试在删除操作中将其发送给 userscontroller。所以我编写了类似的代码

$('document').ready(function(){
        $('.del').click(function(){
            var x=$(this).attr("id");
            alert(x);
            jQuery.get("<?php echo $this->webroot . $this->params["users"]; ?>/delete",{"id":x},function(data,stat){
                                 jQuery("#success").load("success.ctp");
            });
        });
    });

现在在控制器中我已经尝试过删除操作

public function delete($id=NUll) {
        $id=$_GET['id'];
        $this->User->id = $id;
        if ($this->User->delete()) {
            $this->Session->setFlash(__('The user has been deleted.'));
        } 
        return $this->redirect(array('action' => 'index'));
    }

这里不起作用。在控制器中我该如何定义它?

【问题讨论】:

  • 您的浏览器将jQuery.get("&lt;?php echo $this-&gt;webroot . $this-&gt;params["users"]; ?&gt;/delete",{"id":x},function(data,stat){ jQuery("#success").load("success.ctp"); }); 生成为 HTML 的内容?

标签: cakephp cakephp-2.4


【解决方案1】:

如果你使用前置过滤器,

function beforeFilter()
{
    //Here set Which pages should be accessable to various users
    $adminPages =array('delete');
    $this->Auth->allow($adminPages);

    ...... 
}

允许使用 Auth allow 执行它。

如果也可以正常工作,请检查 AJAX 字符串是否正确。使用像abc.com/Users/delete/id:2 这样的手动删除进行检查,只需复制粘贴字符串你就可以输入jQuery.get()。它是否正确删除??

如果一切都无法正常工作,请尝试以下操作::)

public function delete($id=NUll) {
    $id=$_GET['id'];
    $this->User->id = $id;
    if ($this->User->delete()) {
        $this->Session->setFlash(__('The user has been deleted.'));
    } 
    return $this->redirect(array('action' => 'index'));
}

还有 ajax:

        jQuery.get("<?php echo $this->webroot . $this->params["users"].'/delete/';?>"+x+"",{"id":x},function(data,stat){
                             jQuery("#success").load("success.ctp");
        });

【讨论】:

    【解决方案2】:

    这里的错误只是网址,我已经更改了它,现在它工作正常

    echo $this->webroot . $this->params["users"]; ?>/delete"
    

    echo Router::url(array('controller'=>'users','action'=>'delete'));?>"
    

    【讨论】:

      【解决方案3】:

      如果你想使用 ajax 试试这个

         public function delete_user() {
              $this->autoRender = false;
              if ($this->request->data) {
      
              $this->User->id = $this->request->data['id'];
              $update = $this->User->saveField($this->request->data['field'], $this->request->data['value']);
              if ($update) {
                  $repsonce['success'] = '1';
                  $this->Session->setFlash(__d('User', 'User Deleted successfully'), 'default', array('class' => 'alert alert-success'));
              } else {
                  $repsonce['success'] = '0';
              }
          } else {
      
              $repsonce['success'] = '0';
          }
          echo json_encode($repsonce);
      }
      

      【讨论】:

        猜你喜欢
        • 2016-02-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 2014-04-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多