dougai0138 2011-06-15 01:14
浏览 36
已采纳

将PHP对象传递给javascript [复制]

This question already has an answer here:

I have objects in php that each represents a "item" and all info associated with it.

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.

However, how do you get an object from php to javascript?

</div>
  • 写回答

4条回答 默认 最新

  • dongliyun3301 2011-06-15 01:37
    关注

    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:

    1. Convert the PHP object to an array.

    2. Using json_encode() to encode that PHP array.

    3. Pass JSON-encoded data to PHP

    4. Decode JSON from JavaScript using JSON.parse or you can use jQuery.parseJSON to do that.

    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.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法