使用$ .ajax post将cakephp视图中的数据发送到控制器

时间:2022-01-24 17:03:25

I am using $.ajax for the first time in CakePhp2.4.5- I read a lot of Posts on * 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

我在CakePhp2.4.5中第一次使用$ .ajax-我在*上阅读了很多帖子,并在w3schools和jquery网站上做过其他研究,但无法理解我应该如何解决我的问题。我想从第一个视图index.ctp向Controller发送数据

 $.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'.

警报节目'AjaX Success'。

in my Controller I have

在我的控制器中我有

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

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

in my SECOND view client.ctp I have.

在我的SECOND视图client.ctp我有。

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

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

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

问题。我总是在Client.ctp视图中获取msg“ajaxTest not Set”。我究竟做错了什么 ?或者怎么做?谢谢

1 个解决方案

#1


2  

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

我认为你的问题在于网址,因为'meers / client'不在路线中

  $.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!');
        });

you can see other examples:

你可以看到其他例子:

http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/

http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/

Making jquery ajax call from view to controller in cakephp 2.x

在cakephp 2.x中从视图到控制器进行jquery ajax调用

#1


2  

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

我认为你的问题在于网址,因为'meers / client'不在路线中

  $.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!');
        });

you can see other examples:

你可以看到其他例子:

http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/

http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/

Making jquery ajax call from view to controller in cakephp 2.x

在cakephp 2.x中从视图到控制器进行jquery ajax调用