cakephp 2.x 在 javascript 中执行控制器

cakephp 2.x execute controller in javascript

我有以下问题:在 cakaphp 中不再建议使用 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',

通常在核心 php,我会这样做:

$("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