Cakephp ajax post 请求出错
Cakephp with ajax post request get error
我在 Cakephp 中有一个由 ajax 请求加载的表单。通过另一个 ajax 请求发送表单后,魔术在控制器中完成,控制器正在发回新的 html 代码。到目前为止,一切都很好。只要我发送一个获取请求,它就可以很好地工作。如果我更改 ajax 脚本以强制执行 post 请求,结果是黑洞。我阅读了很多有关安全组件的内容,并尝试更改了很多内容,但都没有用。
这是我的"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> </p>
<?php
$formButtonOptions = array(
'type' => 'submit',
'id' => 'btnSave',
'div' => false,
'class' => 'btn btn-xl'
);
echo $this->Form->button(__('Offerte anfordern'), $formButtonOptions);
?>
<p> </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>
这是我的模特"Offer.php":
class Offer extends AppModel {
public $name = 'Offer';
}
感谢您的帮助和提示。有什么想法吗?
将以下内容添加到 beforeFilter
if ($this->action === 'create form') {
$this->Security->validatePost = false;
$this->Security->csrfCheck = false;
}
这应该可以解决您的问题
我不得不更改此行
//$("#btnSave").click(function(e){
$("#ajaxForm").submit(function(e){
在第一个版本(上面的主要问题)中,我使用了#btnSave,现在我尝试使用注释行#ajaxForm。现在可以了。我不知道为什么我在第一个版本中没有使用这一行。不过现在问题已经解决了。
试试这个
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);
}
}
我在 Cakephp 中有一个由 ajax 请求加载的表单。通过另一个 ajax 请求发送表单后,魔术在控制器中完成,控制器正在发回新的 html 代码。到目前为止,一切都很好。只要我发送一个获取请求,它就可以很好地工作。如果我更改 ajax 脚本以强制执行 post 请求,结果是黑洞。我阅读了很多有关安全组件的内容,并尝试更改了很多内容,但都没有用。
这是我的"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> </p>
<?php
$formButtonOptions = array(
'type' => 'submit',
'id' => 'btnSave',
'div' => false,
'class' => 'btn btn-xl'
);
echo $this->Form->button(__('Offerte anfordern'), $formButtonOptions);
?>
<p> </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>
这是我的模特"Offer.php":
class Offer extends AppModel {
public $name = 'Offer';
}
感谢您的帮助和提示。有什么想法吗?
将以下内容添加到 beforeFilter
if ($this->action === 'create form') {
$this->Security->validatePost = false;
$this->Security->csrfCheck = false;
}
这应该可以解决您的问题
我不得不更改此行
//$("#btnSave").click(function(e){
$("#ajaxForm").submit(function(e){
在第一个版本(上面的主要问题)中,我使用了#btnSave,现在我尝试使用注释行#ajaxForm。现在可以了。我不知道为什么我在第一个版本中没有使用这一行。不过现在问题已经解决了。
试试这个
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);
}
}