【问题标题】:CakePhp and jQuery not workingCakePhp 和 jQuery 不工作
【发布时间】:2013-10-24 20:00:00
【问题描述】:

我对 CakePhp 还是很陌生,所以如果我遗漏了什么,请告诉我。我正在尝试创建一个使用 ajax 回发到控制器的表单。这是我所拥有的:

模型:EventExampleModel

class EventExampleModel extends AppModel {

}

查看:/app/View/EventExample/index.ctp

<?php

$data = $this->Js->get('#CommentSaveForm')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#CommentSaveForm')->event(
    'submit',
    $this->Js->request(
        array('action' => 'save'),
        array(
            'update' => '#commentStatus',
            'data' => $data,
            'async' => true,
            'dataExpression'=>true,
            'method' => 'POST'
        )
    )
);
echo $this->Form->create('Commenter', array('action' => 'save', 'default' => false));
echo $this->Form->input('Commenter.comments_name');
echo $this->Form->input('Commenter.comments_text');
echo $this->Form->end(__('Submit'));
echo $this->Js->writeBuffer();

?>

<div id="contactStatus">


</div>

控制器:EventExampleController:

class EventExampleController extends Controller {
  public $helpers = array('Html', 'Form');
    public function index() {
        $this->layout = 'homePage';
    }
    public function save() {

        if ($this->request->is('ajax')) {
            $this->render('contact-ajax-response', 'ajax');
        }
    }
}

和我的布局:

<?php
$cakeDescription = __d('cake_dev', 'Employee Database');
?>
<!DOCTYPE html>
<html>
<head>

    <title>
        <?php echo $cakeDescription ?>
    </title>
    <?php
    echo $this->Html->meta('icon');
    echo $this->Html->css('styles');
    echo $this->fetch('meta');
    echo $this->fetch('script');
    echo $this->Html->script('jquery-1.10.2.js'); // Include jQuery library
    echo $this->Html->script('myScripts');
    ?>
</head>
<body>
<div class="homepage-container">
    <div id="content">
        <?php echo $this->fetch('content'); ?>
    </div>
    <div id="footer">

    </div>
</div>
</body>
</html>

我的 jquery 库也位于 app/webroot/js 文件夹中。

问题是当页面呈现并且我单击提交按钮时,没有任何反应。我在页面或控制台中没有看到任何错误,并且页面没有回发。我错过了什么?我希望页面异步回传到服务器,并且我的保存函数应该被调用(断点永远不会被击中)。

有人可以帮忙吗?

【问题讨论】:

    标签: jquery cakephp


    【解决方案1】:

    您的型号名称错误

    正确的约定是

    EventExample extends AppModel
    

    还有文件名 EventExample.php

    只是一个提示。

    在 google chrome 中打开您的 firebug 或等效软件,然后检查网络选项卡。 请检查单击按钮时是否触发了什么。

    【讨论】:

    • 您好,感谢您的回复。我按照建议更改了模型。但是,当我单击按钮时,仍然没有任何反应。我什至打开了开发人员工具并查看了“网络”选项卡,但什么也没有。关于我可能遗漏的任何建议?
    猜你喜欢
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 2012-05-06
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多