2015-05-04 27 views
-3

我正在使用cakePHP 3.0,并在使用ajax时遇到问题。事实上,我想执行的操作我的阵列控制器上的“SpecimensController”从我的观点来的“viewTab”“标本”CakePHP - 如何将URL传递给ajax?

<script type="text/javascript" > 
var tab = new Array(); 
function updateResult(){ 
     $.ajax({ 
      type:"POST", 
      url:"<?php echo Router::url(array('controller'=>'specimens','action'=>'index'));?>", 
      data:{datas: $("select[name='filtreVariable\\[\\]']").map(function(){return $(this).val();}).get()}, 
      dataType: 'text', 
      async:false, 
      success: function(data){ 
       alert('success'); 
      }, 
      error: function (data) { 
       alert("error"); 
      } 
     }); 
} 

$("#filtre").submit(function(){ 
    updateResult(); 
}); 
</script> 

行动“viewTab”只是做:

echo "success"; 

但我无法找到正确的URL,因此可以调用函数成功。我尝试了很多东西,并且总是有被调用的ajax的函数错误。 :/

+0

尝试调试像'错误:功能(jqXHR,textStatus,errorThrown){警报( 'errorThrown')};' –

+0

你是什么意思约“但是我无法找到正确的URL,因此可以调用函数成功。” ?你找不到哪个网址? – Sojtin

+0

[Ajax not calling success function]可能的重复(http://stackoverflow.com/questions/30002886/ajax-not-calling-success-function) – ndm

回答

0

如果你还没有,请将这行代码在你的AppController在您的初始化函数是:

public function initialize() 
{ 
    parent::initialize(); 
    $this->loadComponent('RequestHandler'); 
} 

在你的控制器,你通过AJAX调用该函数,你可以试试这样的:

public function index() 
{ 
    $data = ['testing']; 
    $this->set(compact('data')); 
    $this->set('_serialize', 'data'); 
} 

http://book.cakephp.org/3.0/en/controllers/components/request-handling.html

+0

嘿谢谢你的回答。但我已经这样做了:/ – Salecoune