简体   繁体   English

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

[英]cakephp 2.x execute controller in javascript

I have the following issue: in cakaphp does not advice anymore the using of jshelper,insted of normal 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
))
))
);
?>

I don't know how to execute cake controller in js(without using jshelper): 我不知道如何在js中执行蛋糕控制器(不使用jshelper):

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

Normally in core php, i would do like this: 通常在核心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);
    });
});

But how to use controller in javascript(jquery)?I am not sure but will it works if i do like this: 但是如何在javascript(jquery)中使用控制器?我不确定,但是如果我这样做,它将起作用吗:

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

You could get the url like this using htmlhelper then it was would be a normal jquery call. 您可以使用htmlhelper获得类似的网址,这将是一个普通的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 http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM