jquery - CakePHP:Ajax 请求的 Controller 响应错误

标签 jquery ajax cakephp cakephp-1.3

我正在使用 jQuery 向某些 Controller 操作发出 AJAX 请求。该请求是通过同一 Controller 中编辑操作 View 中的按钮激活的。

我的问题: Ajax 请求返回编辑 View 的所有代码(包含所有表单和输入),而不是预期的数量。如果我将相同的 ajax 按钮放在添加操作的 View 中,它会完美工作(它返回数字)。

编辑和添加操作保持默认生成的状态(使用烘焙)。

这是发出ajax请求的jQuery函数

        $.ajax({
            type: 'POST',
            url: 'checkTargets',
            data: {target: tgt_array, channel: channel_type},
            success:function(data){
                $('#num_subscribers > span').html(data);
            },
            error:function(){
                $('#num_subscribers > span').html("The subscribers could not be loaded");
            },
            timeout: 5000
        });
    } 

这就是 Action

function checkTargets() {
        if ($this->RequestHandler->isAjax()) { 
            if(!empty($this->params['form'])) {
                $data = $this->params['form'];

                if ($data['channel'] === 'SMS') {
                    $channel = 'sms';
                } else {
                    $channel = 'pin';
                }

                $targets = $this->processPostTargets($data['target']);
                $this->RequestHandler->respondAs('text');
                //This echo a NUMBER
                echo ClassRegistry::init('Selection')->countSubscribersInTarget($channel, $targets);

                Configure:: write('debug', 0);
                $this->autoRender = false;
                exit();

            }
        } 

    }

为什么会发生这种情况?

谢谢

最佳答案

只是一个建议,无法访问所有内容,您尝试过吗?

$this->autoRender = false;
$this->layout = 'ajax';

此外,我建议保持简单:

$.post("/controller/checkTargets", function(data) {
   alert(data);
}

function checkTargets() {
  $this->autoRender = false;
  $this->layout = 'ajax';
  echo "Im working";
}

然后从那里开始。

关于jquery - CakePHP:Ajax 请求的 Controller 响应错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3623738/

相关文章:

jquery - 如何在 ASP.NET MVC 中执行 Ajax/JQuery 上传图像?

php - 将动态字段数据插入数据库问题?

jquery - 我怎样才能改变图像在 Jquery 中的位置?

javascript - 检查是否选择了选项 Form cakephp 3

mysql - 跨多个表选择不同的查询

mysql - 执行数据库行 DeleteAll,其中 1 个字段等于某物而其他字段不等于某物

jquery - 将分钟添加到输入字段并显示小时/分钟

javascript - 如何从客户端javascript调用 Node 函数? (HTML)

javascript - 谷歌日历用户界面

php - PHP/AJAx 实时搜索的最佳方法 - xml 还是 MySQL?