cakephp isAjax + jquery = funny stuff
in the course of writing an article about good cakephp ajax i found myself stumped by bizzare behaviour.
in a nutshell, i was forcing a http 500 error through a test action in a test controller using the following code:

which, by all intents an purproses should throw up a 500 code with my text template shown. And it did (after i tracked a bunch of empty whitespace throughout cakes libraries, gar!).
it did but it would only throw a 500 if you loaded the page through the browser. load it through an ajax request (jquery) and it shat the bed with a http 200 ok code. I confirmed the headers generated by the browser in both instances to find what was different:
firefox alone sends these headers:
[Host] => new-nectar
[User-Agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5
[Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
[Accept-Language] => en-gb,en;q=0.5
[Accept-Encoding] => gzip,deflate
[Accept-Charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
[Keep-Alive] => 300
[Proxy-Connection] => keep-alive
[Cookie] => CAKEPHP=295ec29bde8773d0c880d0554a9b1a01
[Cache-Control] => max-age=0
jquery sends these headers:
[Host] => new-nectar
[User-Agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5
[Accept] => */*
[Accept-Language] => en-gb,en;q=0.5
[Accept-Encoding] => gzip,deflate
[Accept-Charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
[Keep-Alive] => 300
[Proxy-Connection] => keep-alive
[X-Requested-With] => XMLHttpRequest
[Referer] => http://new-nectar/admin/photos/edit/20
[Cookie] => CAKEPHP=d2701bfc43c87a98dfea2b066ee33e8f
the obvious one highlighted. So I started following that through the cakephp maze. First stop \cake\libs\controller\components\request_handler.php. fun!

by adding the early return false, i sidestep any isAjax whoopee being done after the fact, and guess what. It works. Now either type of request serves up a 500 error. interesting stuff. So I tried to follow the chain down further to establish exactly where things were going awry but i quickly found myself in a mess of code I wasn’t interested in dealing with. A quick search of ajax related code in the base gives back a lot of responses, most of which are easy to rule out, but i found that i quickly got lost in the myre. cake’s internals are so rife with magic relationships between values and functions it’s painful to debug. This is one major issue i find repeatedly under the covers of cake, and nothing will fix that because its the core.
having said all that, initial tests show that i can just turn that sucker off and it works without poorly effecting my other code. i lose the ability to magically determine an incoming ajax request but thats fine, i can do it manually or just plan around it. what i can’t have is some vagabond code throwing some whitespace in somewhere and breaking the headers thus forcing me to write very sloppy ajax.
and my guess, which i feel strongly would be correct is somewhere in there is a bad bit of code echoing something, or pushing an early header which results in the redirect headers never making it to the browser (like birth, you can only do it once). But thats on the guys at cake to fix.
if i hadn’t wasted so much time i would post a bug to cake (and figure out how to do that, ahem). but as it is, i blew out the night solving something that should work in the first place.. and makes me so miss symfony’s $this->getResponse()->setStatusCode(500); which worked every single time i called it without fail.
btw..1.2.0.7296 RC2
now, i can finish my next article about how to do good jquery ajax with cake.. soon.
1 year ago