cakephp 控制器的 jquery getJSON() 响应为空,但 200 成功
我可以从标准 http:// 传递错误,但无法在 jquery 调用中获取要渲染(甚至传递)的 $errors 值。有人知道我在这里做错了什么吗?我已经这样做了大约 5 个小时,然后就睡着了。感谢您的指导和批评。
我的 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){
});
});
控制器操作:
function ajax_search_validate() {
if ($this->Plan->validates()) {
$this->Plan->set($this->data);
$errors = $this->Plan->invalidFields();
$this->set('errors', $errors);
}
}
视图(发布此内容可能毫无意义,但嘿……):
<?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 响应:
ResponseHeaders
Date Wed, 18 May 2011 09:28:51 GMT
X-Powered-By PHP/5.2.6-1+lenny10
P3P CP="N..."
Connection Keep-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
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 Keep-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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在呈现的视图是 HTML,因此您的
$.getJSON
调用会获取一堆 HTML,而不是 JSON。如果 JSON 未验证,响应将显示为空,这就是为什么您在$.getJSON
调用中看不到任何返回的原因(尽管有 200 成功响应)。尝试一下:如果您确实希望将 JSON 返回给客户端,则需要修改控制器以返回一些 JSON。类似于:
您可能必须禁用默认视图渲染行为(或创建 JSON 视图)。我对 CakePHP 不熟悉,所以我无法真正详细了解如何实现这一点。
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:If you really do want JSON to be returned to the client, you will need to modify your controller to return some. Something like:
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.