在回发时将服务器端对象数组传递给客户端

发布于 2024-11-17 18:16:46 字数 321 浏览 4 评论 0原文

我正在使用 Google Analytics 来跟踪电子商务。结账页面是动态的,有 3 个不同的视图(最后一个是成功交易的确认)。在每个视图结束时,执行回发以进行服务器端计算,然后显示下一个视图。

有关销售的信息保存在 Product 对象数组中,并且包含(大部分)我想要传递给 Google Analytics 的所有信息。问题是我需要从这个对象数组获取所有内容到客户端,以便我可以在 JavaScript 中循环它并将其传递给 Google。

将这些数据从服务器获取到客户端进行 JavaScript 处理的最简单方法是什么?对象数组中的数据直到第二次回发才最终确定,并在第三次回发后被销毁。

I'm using Google Analytics to track eCommerce. The checkout page is is dynamic with 3 different views (the final being the confirmation of a successful transaction). At the end of each view, a postback is performed to do server-side calculations, then the next view is displayed.

The information about the sale is saved in an array of Product objects and contains (for the most part) all of the information I would like to pass Google Analytics. The problem is that I need to get everything from this object array to the client so I can loop through it in javascript and pass it to Google.

What would be the simplest way to get this data from the server to the client for javascript processing? The data within the object array is not final until the 2nd postback and is destroyed after the 3rd postback.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

‘画卷フ 2024-11-24 18:16:46

我不确定我是否完全理解您所询问的情况,但从服务器获取数据到客户端的最简单方法是从客户端请求数据并让服务器返回 JSON 格式的数据块(通常是数组)或对象)。由于 JSON 只是 javascript,因此客户端可以对返回的数据执行 JSON 安全的 eval,并立即拥有表示所有数据的 javascript 数据结构。根据您在客户端/服务器上使用的库,库中通常内置有 JSON 辅助函数,以使两端变得更加容易(尽管从头开始编写代码也很容易)。

I'm not sure I fully understand the situation you're asking about, but the simplest way to get data from server to client is to request the data from the client and have the server return a JSON formatted block of data (often an array or object). Since JSON is just javascript, the client can do a JSON-safe eval on the returned data and instantly have a javascript data structure that represents all the data. Depending upon what library you are using on client/server, there are usually JSON helper functions built-in to the library to make both ends even easier (though writing the code from scratch is easy too).

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