如何返回两组数组数据以供 JQuery AJAX 访问?
我正在学习 cakePHP 1.26。和 JQuery
在控制器中,我用这些代码行得到了两个操作:
function testing(){
$a = $this->User->findallByuser_id(1);
$b = $this->User->Post->findallByuser_id(1);
return a+b; // I was trying to return Array data $a and $b
}
如何返回两组数组数据($a 和 $b)以供 JQuery AJAX 访问?
如果可以的话请帮忙。
I am learning cakePHP 1.26. and JQuery
In a Controller, I got two an action with these lines of codes :
function testing(){
$a = $this->User->findallByuser_id(1);
$b = $this->User->Post->findallByuser_id(1);
return a+b; // I was trying to return Array data $a and $b
}
how would you return two groups of Array data ($a and $b) to be accessed by JQuery AJAX?
Please help if you could.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这本质上是尼克的回答,我完全同意他的观点,但我认为一个例子可能有用:
这就是我所做的,我发现它非常有效。
This is essentially Nik's answer and I agree with him completely, but I think an example may be useful:
This is what I do and I've found it to be very effective.
我会使用 json_encode/json_decode 当你访问它时,而在客户端,您可以使用 $.parseJSON(json_strong) 将其转换为对象。
I would use json_encode/json_decode and when you access it on you, while at the client side you can use $.parseJSON(json_strong) in order to convert it to object.
您需要在用户和他的帖子之间建立关系,也许是 HasMany 关系。设置完成后,您需要这样的查询:
请参阅 http://book.cakephp。 org/view/1323/Containable 了解有关使用 Containable 的信息。另请参阅 http://book.cakephp.org/view/1039/ Associations-Linking-Models-Together 了解有关通过关联链接模型的信息。
希望有帮助。
You need to have a relation between the user and his posts, maybe with a HasMany relation. When you have that set up, you need a query like this
See http://book.cakephp.org/view/1323/Containable for information on using Containable. Also see http://book.cakephp.org/view/1039/Associations-Linking-Models-Together for information on linking models via associations.
Hope that helps.