0

In my CakePHP view, I call method in controller, using jQuery:

function placeSensors(nk) { $.ajax({ type:'post', url:'/myapp/maps/placeSensors/' + nk, success: function(r) { if(r.status = 'ok') { } } }); } 

JS in controller is defined with ie.:

class MapsController extends AppController { var $name = 'Maps'; var $helpers = array('Js'); var $uses = array('Measurings', 'Maps'); var $components = array('RequestHandler'); // added later, but still the same function index( $id = null, $value = null ) { $code = ''; ?> <script type="text/javascript"> alert('Hello!'); </script> <?php return $code; } 

So, with simple code, I can not get alert message on my web form. Very simple code I was using in some other project and it works there, and for some reason this does not work on this one...

I'm really stuck with this one, can you please help me.....

UPDATE: this is response i'm getting by Firebug:

<script type="text/javascript"> alert('Hello!'); </script> 
2
  • What response do you get if you check it with a tool like FireBug? Commented Sep 3, 2011 at 5:17
  • please check updated part of my message, I do get response from controller in Firebug Commented Sep 3, 2011 at 8:26

3 Answers 3

1

You're trying to place code that should be viewed (javascript) by the client, inside a controller. Controller is for business logic, that the client doesn't see.

Place your javascript inside a javascript file in the /webroot/js/ directory.

For interacting with ajax, tell your controllers to use the RequestHandler component to determine that they're being called by ajax. From there you can return simple values, or return a json or xml view.

If that sounds complicated, don't worry about it for now and just start as simple as you need and slowly build up your application.

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

Comments

1
function placeSensors(nk) { $.ajax({ type:'post', dataType:'json', url:'/myapp/maps/placeSensors/' + nk, success: function(r) { if(r.status) { alert(r.code); } } }); } class MapsController extends AppController { var $name = 'Maps'; var $helpers = array('Js'); var $uses = array('Measurings', 'Maps'); function index( $id = null, $value = null ) { } // update below code function placeSensors( ) { $nk = $_POST['nk']; echo json_encode(array( 'status' => true, 'code' => "code" )); exit(); } 

3 Comments

var $helpers = array('Html', 'Form', 'Javascript', 'Ajax'); tried also all combination of those. still the same......
check my updated code because ur url is "/myapp/maps/placeSensors/" then i create action "placeSensors" in controller.
still the same, same response, but no alert window
0

Ok, i finally solved this one.

All I needed was to add one form and one text, or even better hidden element on form.

After that everything started to work.

So my mentioned code was ok, all I needed was that one text box......... Hope that somebody can tell me why?

Thank you on all your help and support.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.