使用 cakephp 2.3.0 的简单 ajax 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14768635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:56:32  来源:igfitidea点击:

simple ajax example with cakephp 2.3.0

ajaxcakephp

提问by casper

please help me, if anyone can give me an example how to use ajax with cakephp 2.3.0 an example is like this

请帮助我,如果有人能给我一个例子,如何在 cakephp 2.3.0 中使用 ajax,一个例子是这样的

<?php echo $this->html->link('Original', '#', 
                array('onclick'=>'return false;', 'id'=>'remanufactured-link', 'class'=>'get-type-product-link')); ?>   

<div id="content">
</div>

when i click a originallink a div with id contentis change. how can i do it this with cakephp 2.3.0?

当我点击一个original链接时,一个带有 id 的 divcontent发生了变化。我怎么能用cakephp 2.3.0做到这一点?

采纳答案by Vijay Choudhary

See below example:

见下面的例子:

         $.ajax({
            dataType: "html",
            type: "POST",
            evalScripts: true,
            url: '<?php echo Router::url(array('controller'=>'your-controller','action'=>'your-action'));?>',
            data: ({type:'original'}),
            success: function (data, textStatus){
                $("#div1").html(data);

            }
        });

回答by RichardAtHome

There's not much difference to using CakePHP with Ajax than with regular HTML/PHP and Ajax.

将 CakePHP 与 Ajax 一起使用与使用常规 HTML/PHP 和 Ajax 没有太大区别。

Here's an example of an ajax call and response in a Cake APP:

以下是 Cake APP 中的 ajax 调用和响应示例:

jQuery:

jQuery:

$.ajax({
    url: '/types/fetch/original',
    cache: false,
    type: 'GET',
    dataType: 'HTML',
    success: function (data) {
        $('#context').html(data);
    }
});

(Don't forget to change the url parameter to match your setup).

(不要忘记更改 url 参数以匹配您的设置)。

This will make an ajax request to your Types controller and call the method fetch() with the parameter 'original'.

这将向您的类型控制器发出 ajax 请求,并使用参数“原始”调用方法 fetch()。

Your TypesController would look something like this:

你的 TypesController 看起来像这样:

class TypesController extends AppController {

    public $components = array(
        'RequestHandler'
    );

    public function fetch($type) {

        $data = $this->Type->find('all', array(
            'conditions'=>array(
                'Type.type'=>$type
            )
        );

        $this->set('data', $data);

    }
}

Adding the RequestHandler component means Cake will automatically use the minimal Ajax layout when rendering your Ajax requests. Usually this is added in AppController so all Controllers can use it.

添加 RequestHandler 组件意味着 Cake 在呈现您的 Ajax 请求时将自动使用最小的 Ajax 布局。通常这是在 AppController 中添加的,以便所有控制器都可以使用它。

And the associated view:

以及相关的视图:

/app/View/Types/fetch.ctp

/app/View/Types/fetch.ctp

<ul>
    <?php foreach($data as $item): ?>
    <li><?php echo $item['Type']['name']; ?></li>
    <?php endforeach; ?>
</ul>

回答by savedario

I just started myself with cakePHP and Ajax, but from what I gathered so far this seems the easier approach. In your view use:

我刚开始使用 cakePHP 和 Ajax,但从我目前收集的信息来看,这似乎是更简单的方法。在您看来,使用:

echo $this->Js->link(
    'Original',
    '#',
    array('async' => true,
          'update' => 'content',
                  'id' => 'remanufactured-link'
        )
);
<div id="content">
</div>

Which implies a few things about the controller you are calling this view from (e.g. the index action will be invoked by clicking the link etc...).

这意味着您正在调用此视图的控制器的一些事情(例如,将通过单击链接等调用索引操作...)。

回答by Crsr

You can do with jquery/ajax

你可以用 jquery/ajax

$("#remanufactured-link").click(function(){ whatyouneed.appenTo("#content"); })

whatyouneed can be a data response for a post/get or something else.

whatyouneed 可以是 post/get 或其他内容的数据响应。