2016-01-27 155 views
0

我試圖發送post值到OrderController(我使用ZF2)。我有鑑於folder.These JavaScript代碼是代碼:不能參考控制器功能ZF2

function submitHandler(form) { 
     var urls = '<?php echo $this->baseurl; ?>/order/saveOrder'; 
     var pack= $("#pack").val(), 
      name= $("#name").val(), 
      instit= $("#instit").val(), 
      telp= $("#telp").val(), 
      email= $("#email").val(), 
      address= $("#address").val(), 
      orderno = "test", 
      order_stat ="phone", 
      pack_type = "recurring", 
      rec_period ="monthly"; 
     $.ajax({ 
      type: 'POST', 
      url: urls, 
      data: { 
       pack: pack, 
       name: name, 
       instit:instit, 
       telp: telp, 
       email: email, 
       address: address, 
       orderno : orderno, 
       order_stat :order_stat, 
       pack_type : pack_type, 
       rec_period :rec_period 
      }, 
      success: function(msg) { 
       var result = JSON.parse(msg);   
       if (result.success) {   
        alert("SUccESSSS"); 
       } else { 
        alert(result.msg); 
       }   
      }, 
      error: function(xhr, status, error){ 
       alert("Failed"); 
      } 
     }); 
    } 

而這些代碼控制器:

public function saveOrderAction() { 
     $post = $this->getRequest()->getPost(); 
     $response = $this->getResponse(); 
     /* 
     $storage = Order\Storage::factory($this->getDb()); 
     $attd = new Order($storage); 

     $success = false; 
     $msg = ''; 

     $attd->orderno = $post['orderno']; 
     $attd->order_stat = $post['order_stat']; 
     $attd->pack = $post['pack']; 
     $attd->pack_type = $post['pack_type']; 
     $attd->rec_period = $post['rec_period']; 
     $attd->name = $post['name']; 
     $attd->instit = $post['instit']; 
     $attd->telp = $post['telp']; 
     $attd->email = $post['email']; 
     $attd->address = $post['address']; 
     //$save = $attd->saveOrder(); 
     if ($attd->saveOrder(true)) { */ 
      $success = true; 
      $msg = 'Saved Success'; 
      echo "<script type='text/javascript'>alert($msg);</script>"; 
      print_r('test'); 
      printf('test2'); 
     $response->setContent(json_encode(array("success" => $success, "msg" => $msg))); 
     return $response; 
    } 

但它不會顯示任何東西。所以我評論過,只是想顯示警報,但沒有顯示。幫助我,謝謝。

+0

你應該先檢查你的控制器動作是否有錯誤, – PrinceG

+0

我試圖在動作中顯示警報,但它不能。我不知道如何檢查它。 @PrinceG –

+0

你可以試試'var_dump($ post);退出;' – PrinceG

回答

0

也許你需要你的網址改爲:

var urls = '<?php echo $this->baseurl; ?>/order/save-order'; 

URL在Zend的是小寫。多個單詞用破折號分隔。當請求被處理時,動作是駝峯式的,所以你的動作方法是合適的PSR-2。