Re: Please help with Ajax controller method
Here's how I solved the problem: /app/controllers/test_controller.php ?php class TestController extends AppController { var $uses = array(); var $layout = 'test'; var $helpers = array('Html', 'Javascript'); var $components = array('Json'); function index() { } function getText() { $this-set('text', $this-Json-encode(array('text' = 'Hello World!'))); $this-render('json', 'ajax'); } } ? /app/views/layouts/test.thtml !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd; html xmlns=http://www.w3.org/1999/xhtml; head ?php print $html-charsetTag('UTF-8') ? ?php print $javascript-link('prototype') ? /head body ?php echo $content_for_layout ? /body /html /app/views/test/index.thtml pThe text below is retrieved asynchronously from the server:/p div id=sometext/div script type=text/javascript // ![CDATA[ new Ajax.Request('?php echo $html-url('/test/getText'); ?', {onSuccess:displayText}); function displayText(response, json) { document.getElementById('sometext').innerHTML = json.text; } // ]] /script /app/views/test/json.thtml ?php header(X-JSON: $text); ? I'm using the JsonComponent I just added to snippets: http://cakeforge.org/snippet/detail.php?type=snippetid=74 To use JsonComponent you also need the JSON-PHP library JSON.php in your /vendors directory. So basically in client-side javascript an Ajax.Request calls the getText action in TestController, which creates a PHP array of some text and uses the JsonComponent to encode it in JSON format. That action then renders a json view that just sends a X-JSON header with the JSON text. Then back on the client-side, the onSuccess handler gets a 2nd argument that Prototype magically sticks the JSON data in. You can then use that json object to display the data you got from the server. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php -~--~~~~--~~--~--~---
Re: Please help with Ajax controller method
The point of the original post (and my solution) was to send JSON formatted data back to the client, not just HTML to be inserted in a div. Maybe I should've created a less trivial example... Anyhow my original solution wasn't good, the json.thtml view should be: /app/views/test/json.thtml ?php echo $text; ? instead of sending X-JSON headers, there appears to be a limit as to how much data you can stuff in a header. So then you also need a JSON decoder on the javascript side (unless you trust your data, then just do an eval()) - just use the one at http://www.json.org/js.html. So if you just wanted the controller method called once instead of repeatedly, is that what AjaxHelper::remoteFunction() method does? Does it just generate a Prototype Ajax.Request? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php -~--~~~~--~~--~--~---
Re: Please help with Ajax controller method
Personally, I think putting JSON data in the header is a poor solution, especially when you consider that all Prototype adds is doing the eval( ) for you. It's better to just embed the data in your own custom JS code. And yes, AjaxHelper::remoteFunction is used to generate Ajax.Request and Ajax.Updater calls. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php -~--~~~~--~~--~--~---
Re: Please help with Ajax controller method
Um, I would guess that you can't see anything because it's all in the header... As far as RequestHandler goes, it just detects when a call was made via Ajax, and it switches from the 'default' layout to the 'ajax' layout (which is just a blank layout). If you're making the Ajax call via the Prototype library, just put this in your view: ?=$javascript-object($data, true, 'var data = ', ';'); ? Then, the code will be evaluated on the client side, and the JavaScript 'data' variable will be set with the contents of your array. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php -~--~~~~--~~--~--~---