2
votes

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

in my Controller I have

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

in my SECOND view client.ctp I have.

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

1
How you getting this "ajaxTest not Set"? via firebug or by using direct url? - Fazal Rasel
you can see in my View client.ctp I have an if statement if $ajaxTest is set or not. - Meer
I'm not a CakePHP developer, but shouldn't you use is('ajax') instead of the isAjax? It seems the second syntax is deprecated. Please note that JavaScript is case-sensitive, the datatype property should be dataType. - undefined
@undefined even if I remove this isAjax thing shouldn't it work ? It doesn't. I never get data in my Controller I tried in many different ways. I removed this if statement and put it like this. $this->set('ajaxTest',$this->request->myVal); never get a value passed to ajaxTest variable that I send to client view. - Meer
If the "alert show 'AjaX Success'", then how do you know the response text of the request? Does this mean that you see 'ajaxTest not Set.' for non-ajax request or you debug the request in the browser's console? - undefined

1 Answers

2
votes

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

you can see other examples:

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

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