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 -~--~~~~--~~--~--~---
Please help with Ajax controller method
First, I have tried the documentation, but I'm still lost. I'm running the 1.0 version, so I know I need to upgrade, let me know if that is part of my problem. I have a controller method that will only be used for Ajax calls. I need one method that will return JSON formatted data. I need another to return html. I have my own json helper that converts an array to JSON I tried this for my JSON version: class MyappController extends AppController { var $components = array('RequestHandler'); var $helpers = array('Json'); function getdata() { $this-RequestHandler-setAjax($this); // this next line just gets data from the database and creates a standard key/val array $this-set('data',$this-MyModel-getData()); } }? then I have a getdata.thml view: ?php header('X-JSON: ' . $json-makeObject($data));? This never works, I get nothing in my Ajax.Request call, and if I visit the URL in the browser, I get an empty layout, meaning I get my html header and footer files but nothing in the content area. So, my basic question is, what is the 'bare Ajax layout' mentioned in the Request Handler Component documentation? How do I make a method that only is used to return HTML/data when rendered? How do I make a method that returns JSON data when rendered. Any help is appreciated. Brian --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---