I can pass my errors from standard http://, but I cannot get the $errors values to render (or even pass) in my jquery call. Anyone know what I am doing wrong here? I've been at this for about 5 hours and falling asleep on my face.. Thanks for any guidance and critique.
My jQuery:
$j("#applicant_age").focusin(function(){
$j(this).css("background-color","#FFFFCC");
});
$j("#applicant_age").focusout(function(){
$j(this).css("background-color","#ffffff");
$j.getJSON('<?php echo $this->Html->url(array('controller'=>'plans', 'action'=>'ajax_search_validate'))?>', function(json){
});
});
Controller Action:
function ajax_search_validate() {
if ($this->Plan->validates()) {
$this->Plan->set($this->data);
$errors = $this->Plan->invalidFields();
$this->set('errors', $errors);
}
}
View (probably pointless to post this but what the hey...):
<?php
foreach ($errors as $error) :
?>
<p id="errorStyle" style="padding: 10px; background-color: #FF3333; color: #ffffff; margin: 0px 0px 10px 0px; "><?php echo $error; ?></p>
<?php endforeach; ?>
FireBug response:
ResponseHeaders
Date Wed, 18 May 2011 09:28:51 GMT
X-Powered-By PHP/5.2.6-1+lenny10
P3P CP="N..."
Connection K开发者_运维技巧eep-Alive
Content-Length 0
Server Apache/2.2.9 .....
Content-Type text/html
Keep-Alive timeout=15, max=100
RequestHeaders
Accept application/json, text/javascript, */*; q=0.01
X-Requested-With XMLHttpRequest
The view you are rendering is HTML, so your $.getJSON
call is fetching a bunch of HTML, and not JSON. The response will appear empty if the JSON does not validate, which is why you are seeing nothing returned in your $.getJSON
call (despite the 200 success response). Try this:
$j.get('<?php echo $this->Html->url(array('controller'=>'plans', 'action'=>'ajax_search_validate'))?>', function(html){
alert(html);
});
If you really do want JSON to be returned to the client, you will need to modify your controller to return some. Something like:
echo json_encode(array('errors', $errors));
You will probably have to disable the default view rendering behaviour (or create a JSON view). I am not familiar with CakePHP so I can't really go into the details as to how that can be achieved.
精彩评论