【问题标题】:how to return an ajax request response(data ) to client in cakephp 2.3如何在 cakephp 2.3 中向客户端返回 ajax 请求响应(数据)
【发布时间】:2013-03-06 21:00:41
【问题描述】:

我正在尝试在 cakephp 2.3 中使用 ajax 添加到数据库,但不知道如何设置响应,而是向用户提供我会使用的其他数据

$this->set()

对于普通请求 查看文件:

        echo $this->Form->create(); echo $this->Form->input('name');
    echo $this->Form->input('email');
    echo $this->Form->input('phone');
    echo $this->Form->input('message');
echo $this->Js->submit('Send Enquiry', array(
    'before' => $this->Js->get('#sending')->effect('fadeIn'),
    'success' => $this->Js->get('#sending')->effect('fadeOut'),
             'update' => '#success',
              'async' => true
               ));echo $this->Form->end();?>

控制器功能是:

public function add() {
    if ($this->request->is('post')) {
        $this->Contact->create();
        if ($this->Contact->save($this->request->data)) {

            if($this->request->isAjax()){
                $this->autoRender = false;  
                echo 'successful';
                }else{
            $this->Session->setFlash(__('The contact has been saved'));
            $this->redirect(array('action' => 'index'));
                }
        } else {
            $this->Session->setFlash(__('The contact could not be saved. Please, try again.'));
        }
    }
}

【问题讨论】:

  • “不知道如何设置响应,而是向用户提供我本来会使用的其他数据” - 很难说出这里问的是什么。你可以尝试重新措辞吗?

标签: php jquery ajax cakephp cakephp-2.3


【解决方案1】:

抱歉,如果我的问题措辞有误,但我找到了解决问题的方法 我使用下面的 sn-p 来获取验证错误。

if($this->request->isAjax()){

        $this->autoRender = false;      
        if($this->Contact->validates()){

        }else{
        $error = implode($this->Contact->validationErrors;
        echo $error;


            }
        }

谢谢大家

【讨论】:

    猜你喜欢
    • 2015-02-02
    • 2021-03-12
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 2021-03-16
    • 1970-01-01
    相关资源
    最近更新 更多