将PHP对象传递给javascript [英] passing PHP objects to javascript
问题描述
我在php中有对象,每个对象代表一个项目以及与之关联的所有信息。
I have objects in php that each represents a "item" and all info associated with it.
当用户浏览页面时,应该传递这些对象到javascript。理想情况下,镜像相同的结构,所以我可以使用Raphael在我的网站上将每个项目及其信息显示为单独的形状。
And when the user browses the page, these objects should be passed to javascript. Ideally, mirroring the same structure, so I can use Raphael to display each item and its info as separate shapes on my site.
但是,如何从中获取对象php到javascript?
However, how do you get an object from php to javascript?
推荐答案
您可以将PHP对象转换为数组,然后使用JSON函数对其进行编码。之后,从JavaScript解码。以下是一些基本步骤:
You can convert the PHP object to an array, and then use JSON function to encode it. After that, decode it from JavaScript. Here are some basic steps:
Convert the PHP object to an array.
使用 json_encode()
对该PHP数组进行编码。
Using json_encode()
to encode that PHP array.
通过 JSON编码数据到PHP
Pass JSON-encoded data to PHP
使用 JSON.parse
或你可以使用 jQuery.parseJSON
来这样做。
Decode JSON from JavaScript using JSON.parse
or you can use jQuery.parseJSON
to do that.
这是一篇关于传递 JavaScript对象到PHP对象。通过观看其他精选/相关视频,您可能会发现它很有用。
This is an interesting tutorial about passing a JavaScript object to a PHP object. You might find it useful by watching some other featured/related videos.
希望这有帮助。
这篇关于将PHP对象传递给javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!