cakehp2.x用javascript执行控制器


cakephp 2.x execute controller in javascript

我有以下问题:在cakapp中,不再建议使用jshelper,而不是普通的javascript。

    <?php
$this->Js->get('#PostCategoryId')->event('change', 
$this->Js->request(array(
'controller'=>'subcategories',
'action'=>'getByCategory'
), array(
'update'=>'#PostSubcategoryId',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
))
);
?>

我不知道如何在js中执行蛋糕控制器(不使用jshelper):

$this->Js->request(array(
'controller'=>'subcategories',

通常在corephp中,我会这样做:

$("select#category").change(function(){
var id = $("select#category option:selected").attr('value');
$.post("select_type.php", {id:id}, function(data){//select_type.php is the "controller"
    $("select#type").html(data);
    });
});

但是如何在javascript(jquery)中使用控制器呢?我不确定,但如果我这样做,它会起作用吗:

$.post("posts/subcategories/getByCategory", {id:id}, function(data){

您可以使用htmlhelper获得这样的url,然后它将是一个普通的jquery调用。

var url = <?php
    echo $this->Html->url(array(
    "controller" => "posts",
    "action" => "view"
));
?>;

http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::url