由于某种原因,如果我将表单元素更改为隐藏,则我的ajax表单不起作用。如果我将它们改为输入,它会如何工作。为什么会这样?
<div id="price">
<?php
$this->Js->get('#phonepricearea');
echo $this->Form->create('offer', array('url' => '/PhoneKarma/PhoneQueries/ajaxOffer', 'class' => 'custom'));
echo $this->Form->hidden('phoneCapacity',array('value'=>''));
echo $this->Form->hidden('phoneCondition',array('value'=>''));
echo $this->Form->hidden('carrier',array('value'=>''));
echo $this->Js->submit('Check', array('class' => 'button expand',
'title' => 'Check',
'url' => array(
'action' => 'ajaxOffer'
),
'update' => '#price'
));
echo $this->Form->end();
?></div>
public function ajaxOffer($capacity=null, $condition = null , $carrier = null) {
if (!empty($this->data) && $this->request->is('ajax')) {
//do stuff this doesn't effect the code..
$this->render('ajaxOffer', 'ajax');
} else {
$this->set('offer', "0");
}
}
$('#offerPhoneCapacity').val(id);
答案 0 :(得分:1)
400错误通常是security component blackholes。文档说它会发出404错误,但这是错误的,it throws a BadRequestException
如果没有另外配置。
如果某个操作受到安全组件的限制,则会出现黑洞 作为无效请求,默认情况下会导致404错误。您 可以通过设置来配置此行为
$this->Security->blackHoleCallback
属性为回调函数 控制器。
SecurityComponent::blackHole(object $controller, string $error)
使用404错误或自定义回调黑洞无效请求。 没有回调,请求将被退出。如果是控制器回调 设置为SecurityComponent :: blackHoleCallback,它将被调用 传递了任何错误信息。
您的问题可能是由安全组件form tampering prevention功能引起的。隐藏字段需要是静态的,因为它们的值用于生成安全令牌,如果值发生更改,则生成的比较令牌将不同,因此表单将被视为无效。
默认情况下,SecurityComponent会阻止用户篡改表单。 它通过使用FormHelper并跟踪哪些文件来完成此操作 在一种形式。它还跟踪隐藏输入元素的值。 所有这些数据被组合并变成哈希。当表格是 提交后,SecurityComponent将使用POST数据构建相同的内容 结构并比较哈希。
如果您需要更改隐藏字段,则必须在unlockedFields
property/option中或使用表单助手unlockField()
方法定义它们。
示例(未经测试):
public $components = array
(
'Security' => array
(
'unlockedFields' => array
(
'Offer.phoneCapacity',
'Offer.phoneCondition',
'Offer.carrier'
)
)
);
public function beforeFilter()
{
$this->Security->unlockedFields => array
(
'Offer.phoneCapacity',
'Offer.phoneCondition',
'Offer.carrier'
);
}
$this->Form->unlockField('Offer.phoneCapacity');
$this->Form->unlockField('Offer.phoneCondition');
$this->Form->unlockField('Offer.carrier');