2

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

6
  • How you getting this "ajaxTest not Set"? via firebug or by using direct url? Commented Oct 10, 2014 at 19:32
  • you can see in my View client.ctp I have an if statement if $ajaxTest is set or not. Commented Oct 10, 2014 at 19:36
  • 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. Commented Oct 10, 2014 at 23:35
  • @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. Commented Oct 10, 2014 at 23:39
  • 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? Commented Oct 10, 2014 at 23:41

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

12 Comments

@#Widrogo thanks for your reply but if I give this function a false route it should give an error as I have tried by giving a false url. But in my code the AjaX returns a SUCCESS. What about it ?
I used your version of URL but it gave me this error in console Uncaught SyntaxError: Unexpected token ILLEGAL and what i have found is Html Helper is now including my root App name in the Url /MyAppName/meers/client , I think it is not accessable.
make a debug in you controller debug($this->request->isAjax()); and for do a request Depends on the version of cake. 1.3.x: $this->ResquestHandler->isAjax(); 2.x $this->request->is('ajax');
I have mentioned my cakephp version in the post it is 2.4.5 and what about the URL ? should I keep the way it is ? I am going to check debug and ->is('ajax'). by the way what if I leave the if statement for checking if it is ajax ? I want first of all see data in controller coming from ajax post.
see in your code you need see 1 the url 2 your request according to version. now is a little difficult to write and revise your code in me phone, I will help you more when I get my pc. while searching in google about this
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.