CakePHP - 设置从 AJAX 调用中检索到的数据到元素
我正在通过 AJAX 进行新的数据输入。它由模型数据和一些 HABTM 数据组成。我从控制器返回所有这些数据作为 json 对象,我想添加一个 CakePHP 视图元素包含当前视图的数据,有点像 twitter 在您发布推文时所做的那样。
如何设法获取 json 对象、附加元素并将数据从 json 对象设置到所述元素?我对如何附加元素有一个好主意,但我不清楚数据设置。
I'm making a new data entry through AJAX. It consists of the model data and some HABTM data as well. I return all this data as a json object from my controller and I would like to add a
CakePHP view element containing this data to the current view, somewhat what twitter does when you post a tweet.
How can I manage to get the json object, append the element and set the data from the json object to said element? I have a good idea about how to append the element but the data setting I'm not clear about.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解你的问题,你想使用 CakePHP 视图来显示 JSON 数据的结果?
如果是这样,你可以这样做:
在你的控制器中:
创建一个视图文件:my_ajax_action.ctp,它输出格式化的$data数组
使用ajax取回HTML而不是JSON<-重要位并将其插入到DOM中:
If I'm understanding your question correctly, you want to use CakePHP views to display the results of the JSON data?
If so, you can do it like this:
In your controller:
Create a view file: my_ajax_action.ctp which outputs the formatted $data array
Use ajax to fetch back the HTML not the JSON <- important bit and insert it into the DOM:
使用
$.getJSON
获取 JSON,然后迭代并设置页面上的值。非常直接..
看看这里
http://api.jquery.com/jQuery.getJSON/
Use
$.getJSON
to get the JSON, and then iterate and set the values on your page.Very straight forward..
Have a look here
http://api.jquery.com/jQuery.getJSON/