2013-05-01 22 views
0

由於某種原因,如果我將表單元素更改爲隱藏,我的ajax表單不起作用。如果我將它們改爲輸入,它確實如此。爲什麼會這樣?爲什麼我的ajax請求不能使用隱藏的AJAX表單輸入CakePHP 2.3

這裏是視圖

<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"); 
     } 
    } 

Javascript來改變值

$('#offerPhoneCapacity').val(id); 
+0

你有沒有嘗試調試與螢火蟲或谷歌Chrome瀏覽器開發的應用工具? – kentverger 2013-05-01 04:23:52

+0

是它給我「無法加載資源:服務器響應狀態爲400(錯誤請求)」 – 2013-05-01 04:30:15

+0

發佈您的ajax和控制器功能 – Preetam 2013-05-01 04:45:45

回答

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');