从CakePHP的视图发送数据使用$阿贾克斯后到控制器 [英] send data from cakephp view to controller using $.ajax post

查看:117
本文介绍了从CakePHP的视图发送数据使用$阿贾克斯后到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用$阿贾克斯首次在CakePhp2.4.5-我读计算器上很多帖子,并做了W3Schools的和jQuery网站等的研究,但不明白我应该怎么解决我的问题。 我想从我的第一个观点index.ctp将数据发送到控制器

I am using $.ajax for the first time in CakePhp2.4.5- I read a lot of Posts on stackoverflow and did other research on w3schools and jquery site but could not understand how should I solve my problem. I want to send data to the Controller from my 1st view index.ctp

 $.ajax({
                type: 'POST',
                url: 'meers/client',
                datatype:'json',
                cache: false,
                data: {myVal:'Success!'},
                success: function(){alert('AjaX Success')},
                error: function(){alert('AjaX Failed')}
            })
                .done(function(){
                    alert('AjaX Done!');
                });

警报显示阿贾克斯成功。

alert show 'AjaX Success'.

在我的控制器我有

公共功能的客户端(){ 如果($这 - >请求 - > isAjax()){             $这个 - >集(ajaxTest','成功');         }}

public function client(){ if($this->request->isAjax()){ $this->set('ajaxTest','Success'); }}

在我的第二个观点client.ctp我。

in my SECOND view client.ctp I have.

如果(使用isset($ ajaxTest)){     回声$ ajaxTest; }其他{     回声ajaxTest没有设置。; }

if(isset($ajaxTest)){ echo $ajaxTest; }else{ echo 'ajaxTest not Set.'; }

问题。我总是味精在我Client.ctp认为ajaxTest未设定。 我究竟做错了什么?或者该怎么办呢?谢谢

PROBLEM. I always get msg in my Client.ctp view "ajaxTest not Set". What am I doing wrong ? or How to Do it ? Thanks

推荐答案

我觉得你的问题是在URL中,因为不是一个路线米尔斯/客户端

i think that you problem is in the url because 'meers/client' in not a route

  $.ajax({
        type: 'POST',
        url: "<?php echo $this->Html->url(array('controller' => 'YourController','action' => 'YourAction')); ?>",
        datatype:'json',
        cache: false,
        data: {myVal:'Success!'},
        success: function(){alert('AjaX Success')},
        error: function(){alert('AjaX Failed')}
    })
        .done(function(){
            alert('AjaX Done!');
        });

或者可以探测给人一种路由器:

or can probe giving a router:

  $.ajax({
        type: 'POST',
        url: "<?php echo Router::url(array('controller' => 'YourController', 'action' => 'YourAction'), true); ?>",
        datatype:'json',
        cache: false,
        data: {myVal:'Success!'},
        success: function(){alert('AjaX Success')},
        error: function(){alert('AjaX Failed')}
    })
        .done(function(){
            alert('AjaX Done!');
        });

您可以看到其他例子:

http://www.dereuromark.de/2014/01 / 09 / AJAX和 - 的CakePHP /

<一个href="http://stackoverflow.com/questions/18440905/making-jquery-ajax-call-from-view-to-controller-in-cakephp-2-x">Making从视图中jQuery的Ajax调用CakePHP中2.X到控制器

这篇关于从CakePHP的视图发送数据使用$阿贾克斯后到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆