Cakephp 3,Jquery Json响应总是出错

时间:2015-06-16 04:03:00

标签: jquery json cakephp cakephp-3.0

我在找出为什么总是收到以下代码的错误响应时遇到了一些麻烦。

我的表格 - 以防万一我在html端有错误,但似乎一切都运行良好。

<?= $this->Form->create($user, ['url' => '/users/edit', 'name' => 'editAccountForm', 'id' => 'editAccountForm', 'class' => 'sky-form']); ?>
    <? $this->Form->templates([
        'inputContainer' => '<div class="account-input">{{content}}</div>'
    ]); ?>
    <div id="response"></div>

    <fieldset>
        <div><?= $this->Form->input('email', ['id' => 'email', 'disabled' => true]); ?></div>
        <div><?= $this->Form->input('firstname', ['id' => 'firstname']); ?></div>
        <div><?= $this->Form->input('lastname', ['id' => 'lastname']); ?></div>
        <div>
            <?
            $gender = ['male' => 'Male', 'female' => 'Female', 'other' => 'Other'];
            echo $this->Form->input('gender', ['id' => 'gender', 'label'=>'Gender', 'type'=>'select', 'options'=>$gender]);
            ?>
        </div>
    </fieldset>
    <footer>
        <?= $this->Form->button(__('Save Changes'), ['type' => 'submit', 'class' => 'button']); ?>
    </footer>
<?= $this->Form->end() ?>

Jquery - 这是我相信我的问题所在。即使我强行成功回复,它也会始终返回警报(&#34;服务器错误&#34;)。

/* Attach a submit handler to the form */
    $("#editAccountForm").submit(function(event) {

        /* Stop form from submitting normally */
        event.preventDefault();

        $.ajax({
            url: "/linux/users/edit.json",
            type: "POST",
            dataType: "json",
            data: $(this).serialize(),
            contentType :   'application/json',
            success: function(data) {
                if(data.status == 'success'){
                    alert("Changes made successfully");
                }else if(data.status == 'error'){
                    alert("Failed saving changes");
                }
            },
            error: function() {
                alert("Server Error");
            }
        });


        return false;

    });

UsersController.php - 不确定请求处理程序是否正在检测ajax请求。

public function edit($id = null)
{

    if (is_null($id)) {
        $id = $this->Auth->user('id');
    }
    $user = $this->Users->get($id, [
        'contain' => []
    ]);
    if ($this->request->is('ajax')) {

        $this->disableCache();
        $this->layout = 'ajax';
        $data = $this->request->data;

        $this->set("status", "OK");
        $this->set("message", "You are good");
        $this->set("content", $data);
        $this->set("_serialize", array("status", "message", "content"));
    }

    if ($this->request->is(['patch', 'post', 'put'])) {
        $user = $this->Users->patchEntity($user, $this->request->data);
        if ($this->Users->save($user)) {
            $this->Flash->success('Information updated.');
            return $this->redirect($this->referer());
        } else {
            $this->Flash->error('The user could not be saved. Please, try again.');
        }
    }
    $this->set(compact('user'));
    $this->set('_serialize', ['user']);
}

ajax.ctp - 默认所以这似乎不是罪魁祸首。

<?= $this->fetch('content') ?>

routes.php - 也在&#34; /&#34;范围

    $routes->extensions(['json', 'xml']);

0 个答案:

没有答案