Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP AJAX request not recognized
    primarykey
    data
    text
    <p>I'm trying to create a favorite/star/heart button (like in Gmail) for each item on a list of elements in CakePHP 2.2.</p> <p>I've got a working controller and a JS toggle function but my problem is that the AJAX parts of my scripts aren't actually happening; instead the page redirects to itself (as part of the fallback for browsers with JavaScript off) and the reloads. This is what I'm trying to avoid so the users don't loose their position on the page.</p> <p>Here's my CakePHP controller that Un-Favorites an item:</p> <pre><code>class MarksController extends AppController { public $components = array('RequestHandler'); public function unfav($id = null) { // set default class &amp; message for setFlash $class = 'flash_bad'; $msg = 'Invalid List Id'; if ( validation($id) ) { //load this mark's data $this-&gt;request-&gt;data = $this-&gt;Mark-&gt;read(null, $id); $this-&gt;request-&gt;data['Mark']['favorite'] = '0'; if ($this-&gt;Mark-&gt;save($this-&gt;request-&gt;data)) { $class = 'message'; $msg = 'The WatchMark has removed from favorites.'; } else { $msg = 'This WatchMark could not be saved. Please, try again.'; } $this-&gt;autoRender= false; $this-&gt;autoRender = $this-&gt;layout = false; // output JSON on AJAX request if($this-&gt;request-&gt;is('ajax')) { echo json_encode(array('success'=&gt;($class=='flash_bad') ? FALSE : TRUE,'msg'=&gt;"{$msg}")); exit; } // set flash message &amp; redirect $this-&gt;Session-&gt;setFlash($msg,'default',array('class'=&gt;$class)); $this-&gt;redirect($this-&gt;referer()); } else { //trying to edit a mark that doesn't belong to this user: throw new NotFoundException(__('Invalid mark')); } // end ownedby check } //end markfav method </code></pre> <p>I have another class that's the inverse function called markfav (combining them into a single toggle function is a task for another day).</p> <p>Then, on the JQuery &amp; JavaScript side:</p> <pre><code>$(document).ready(function() { // class exists if($('.fav_toggler').length) { // add click handler $('.fav_toggler').click(function(e){ $('#flashMessage').fadeOut(); if($(this).hasClass("heart")) { $.post('/marks/markfav/' + $(this).attr('target_id'), "ajax=1", function(response) { if(response.success == true) { $(this).addClass("heart").removeClass("hearttoggle"); } },'json'); } else { $.post('/marks/unfav/' + $(this).attr('target_id'), "ajax=1", function(response) { if(response.success == true) { $(this).addClass("heart").removeClass("hearttoggle"); } },'json'); } e.preventDefault(); }); return false; } }); </code></pre> <p>I've tested by throwing alerts into my JavaScript and I think that it's executing correctly (it gets to the right part of my code and changes the class between heart &amp; hearttoggle.</p> <p>I also know that it's skipping the <code>if($this-&gt;request-&gt;is('ajax'))</code> block in my PHP for some reason (though I have an AJAX delete function that hits a similar conditional just fine). I've tested removing the <code>if($this-&gt;request-&gt;is('ajax'))</code> condition from the PHP block to force it to execute the JSON_encode bit and I just end up with a page containing the JSON encoded array and nothing else.</p> <p>How do I make this work correctly using AJAX without a page reload?</p> <hr> <p><strong>EDIT: Headers from call to /marks/unfav/624</strong></p> <pre><code>Request URL:http://towatchlist.com/marks/unfav/624 Request Method:GET Status Code:200 OK Request Headers Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Connection:keep-alive Cookie:CAKEPHP=blah Host:towatchlist.com Referer:http://towatchlist.com/marks User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_1) AppleWebKit/537.8 (KHTML, like Gecko) Chrome/23.0.1255.0 Safari/537.8 Response Headers Connection:Keep-Alive Content-Encoding:gzip Content-Type:text/html Date:Sun, 09 Sep 2012 20:55:18 GMT Keep-Alive:timeout=5, max=94 Server:Apache/2.2.22 (Ubuntu) Transfer-Encoding:chunked Vary:Accept-Encoding X-Powered-By:PHP/5.3.10-1ubuntu3.2 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload