CakePHP - 设置从 AJAX 调用中检索到的数据到元素

发布于 2024-12-04 13:26:22 字数 200 浏览 5 评论 0原文

我正在通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书信已泛黄 2024-12-11 13:26:22

如果我正确理解你的问题,你想使用 CakePHP 视图来显示 JSON 数据的结果?

如果是这样,你可以这样做:

在你的控制器中:

function my_ajax_action() {

   $data = // whatever method you use to fetch your data
   $this->set(compact('data'));
   $this->layout('ajax');

}

创建一个视图文件:my_ajax_action.ctp,它输出格式化的$data数组

使用ajax取回HTML而不是JSON<-重要位并将其插入到DOM中:

$.ajax({
    url: '/controller/my_ajax_action',
    success: function (result) {
        $('#myelement').html(result);
    }
});

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:

function my_ajax_action() {

   $data = // whatever method you use to fetch your data
   $this->set(compact('data'));
   $this->layout('ajax');

}

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:

$.ajax({
    url: '/controller/my_ajax_action',
    success: function (result) {
        $('#myelement').html(result);
    }
});
你的背包 2024-12-11 13:26:22

使用 $.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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文