Cakephp with ajax post request get error



我在Cakephp中有一个由ajax请求加载的表单。通过另一个 ajax 请求发送表单后,魔术在控制器中完成,控制器发回新的 html 代码。目前为止,一切都好。只要我发送获取请求,它就可以很好地工作。如果我更改 ajax 脚本以强制发布请求,结果是黑洞。我读了很多关于安全组件的信息,并试图改变很多东西,但它不起作用。

这是我的"OffersController.php"代码:

App::uses('AppController', 'Controller');
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');

class OffersController extends AppController {
public $components = array('Paginator');

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('createform');
    $this->Security->unlockedActions = array('createform');
}

public function createform() {
    $this->request->onlyAllow('ajax'); // No direct access via browser URL
    //$this->autoRender = 'false; // We don't render a view in this example
    $this->Security->validatePost = false;
    if ($this->request->is('post')) {
        $this->Session->setFlash(__('Post sent.').' <tt class="pull-right">(C:Offers, F:createform, A:2)</tt>', 'default', array('class' => 'alert alert-success'));
    } else {
    }
    $this->layout = false;
    $this->render('offerform');
}
}
这是将通过ajax加载的

"createform.ctp"(如果加载了页面,然后发送了ajax请求:

        <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h2 class="section-heading">Offerte</h2>
                <h3 class="section-subheading text-muted">Fordere unverbindlich eine Offerte an.</h3>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
                <?php echo $this->Form->create('Offers', array('name' => 'Offer', 'action' => 'createform', 'role' => 'form', 'type' => 'post', 'id' => 'ajaxForm')); ?>
                    <div class="row">
                        <div class="col-md-8">
                            <?php
                                echo $this->Form->input('firstname', array('type' => 'text', 'label' => false, 'div' => 'form-group col-md-6', 'class' => 'form-control', 'placeholder' => 'Vorname'));
                                echo $this->Form->input('lastname', array('type' => 'text', 'label' => false, 'div' => 'form-group col-md-6', 'class' => 'form-control', 'placeholder' => 'Nachname'));
                                echo $this->Form->input('address', array('type' => 'text', 'label' => false, 'div' => 'form-group col-md-6', 'class' => 'form-control', 'placeholder' => 'Adresse'));
                                echo $this->Form->input('zip', array('type' => 'text', 'label' => false, 'div' => 'form-group col-md-6', 'class' => 'form-control', 'placeholder' => 'Postleitzahl'));
                                echo $this->Form->input('city', array('type' => 'text', 'label' => false, 'div' => 'form-group col-md-6', 'class' => 'form-control', 'placeholder' => 'Ort'));
                                echo $this->Form->input('country', array('options' => array('CH' => 'Schweiz', 'DE' => 'Deutschland', 'AT' => 'Österreich'), 'label' => false, 'div' => 'form-group col-md-6', 'class' => 'form-control', 'before' => '', 'between' => '', 'after' => ''));
                                echo $this->Form->input('emailaddress', array('type' => 'text', 'label' => false, 'div' => 'form-group col-md-6', 'class' => 'form-control', 'placeholder' => 'Emailadresse'));
                                echo $this->Form->input('telephone', array('type' => 'text', 'label' => false, 'div' => 'form-group col-md-6', 'class' => 'form-control', 'placeholder' => 'Telefonnummer'));
                                echo $this->Form->input('desireddate', array('type' => 'text', 'label' => false, 'div' => 'form-group col-md-6', 'class' => 'form-control', 'placeholder' => 'gewünschter Reinigungstermin'));
                                $feesarray = Configure::read('feesarray');
                                $currency = 'CHF';
                                $optionroom['0'] = 'Anzahl Zimmer / Preis';
                                foreach($feesarray as $key => $val) {
                                    $value = $key.' Zimmer / '.$currency.' '.$val[$currency];
                                    $optionroom[$value] = $value;
                                }
                                echo $this->Form->input('rooms', array('options' => $optionroom, 'label' => false, 'div' => 'form-group col-md-6', 'class' => 'form-control', 'before' => '', 'between' => '', 'after' => ''));
                            ?>
                        </div>
                        <div class="col-md-4">
                            <?php echo $this->Form->input('message', array('type' => 'textarea', 'label' => false, 'div' => 'form-group col-md-12', 'class' => 'form-control', 'placeholder' => 'Nachricht')); ?>
                        </div>
                    </div>
                    <div class="clearfix"></div>
                    <div class="row">
                        <div class="col-lg-12 text-center">
                            <p>&nbsp;</p>
                            <?php
                                $formButtonOptions = array(
                                    'type' => 'submit',
                                    'id' => 'btnSave',
                                    'div' => false,
                                    'class' => 'btn btn-xl'
                                );
                                echo $this->Form->button(__('Offerte anfordern'), $formButtonOptions);
                            ?>
                            <p>&nbsp;</p>
                        </div>
                    </div>
                <?php echo $this->Form->end(); ?>
            </div>
        </div>
    </div>
    <script>
        $(function () {
            $("#btnSave").click(function(e){
            //$("#ajaxForm").submit(function(e){
                e.preventDefault(); // avoid to execute the actual submit of the form.
                var formData = $(this).serializeArray();
                var formURL = $(this).attr("action");
                $('#ajaxLoadForm').html('<?php echo __('loading ...'); ?>');    // has to be placed after the var formData, because the form gets replaced after this line...
                $.ajax({
                    type: "post",
                    cache: false,
                    dataType: "html",
                    data: formData,
                    //processData: true,
                    url: formURL,
                    success: function (data, textStatus, jqXHR) {
                        // replace div's content with returned data
                        $('#ajaxLoadForm').html(data);
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        alert("An error occurred: " + jqXHR.responseText.message);
                        console.log(jqXHR);
                    }
                });
            });
        });
    </script>

这是我的模型"报价.php":

class Offer extends AppModel {
public $name = 'Offer';
}

感谢您的任何帮助和提示。有什么想法吗?

我不得不更改此行

//$("#btnSave").click(function(e){
$("#ajaxForm").submit(function(e){

在第一个版本(上面的主要问题)中,我讨厌使用 #btnSave,现在我用 outcomment 行 #ajaxForm 尝试了它。现在它起作用了。我不知道为什么我没有在我的第一个版本中使用这一行。但是问题现在已经解决了。

以下内容添加到 beforeFilter

if ($this->action === 'create form') {
    $this->Security->validatePost = false;
    $this->Security->csrfCheck = false;
}

这应该可以解决您的问题

试试这个

 public function edit_comment() {
        $this->layout = 'ajax';
        $this->autoRender = false;
        if ($this->request->is('ajax')) {
            if ($this->FaComment->save($this->request->data, false)) {
                $response['status'] = 'success';
                $response['action'] = 'edit_comment';
                $response['data'] = $this->request->data['FaComment'];
                $response['message'] = __d('vanderdeals', 'Comment saved successfully');
        } else {
            $response['status'] = 'error';
            $response['model'] = 'FaComment';
            $response['message'] = __d('vanderdeals', 'Internal server error occurred. Please try again later.');
        }
        echo json_encode($response);
    }
}

最新更新