是否有与 php 的 urldecode 或 html_entity_decode() 等效的 JavaScript?
我在 php 中有一个关联数组,我想将其粘贴到隐藏的表单输入中,以便稍后可以获取 javascript 中的值。由于引用问题,我需要对 json 或 htmlentity 进行 urlencode。但是,一旦我在 javascript 中获得了对象,将其恢复为普通 json 的最简单方法是什么?
或者有更好的方法来解决这个问题吗? AJAX 在这里似乎有点矫枉过正——我宁愿在页面上预先填充数据,而不是设置多个请求。我想我可以将 php 对象写入 php 生成的脚本标记内的页面,但这看起来有点混乱,而且我不确定 [scripts] 是否会在可能使用它的每种可能情况下被解析。
I've got an associative array in php that I want to stick into a hidden form input so that I can later get the values in javascript. Because of quoting issues, I need to urlencode the json, or htmlentity it. Once I have the object in javascript, though, what's the easiest way to get it back to plain ol' json?
OR is there a better way to approach this issue? AJAX seems like overkill here -- I'd rather pre-populate the data on the page, rather than setting up multiple requests. I suppose I could write the php object to the page inside a php-generated script tag, but that seems a bit messy, and I'm not certain that [scripts] will be parsed in every possible situation where this might be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将数据粘贴在字段中,则可以使用decodeURIComponent 来解码该值。示例:
另一种方法可能是将 JSON 对象嵌入到 PHP 输出的
元素中。您甚至可以将 JSON 对象作为函数的参数,并将其存储在某个地方。
当页面加载时,将使用文字
['hello', 'world']
调用setupData
。If you stick the data within a field you could use
decodeURIComponent
to decode the value. Example:Another approach might be to embed the JSON object in a
<script>
element in the PHP output. You could even go as far as to make the JSON object an argument to a function that stores it away somewhere.When the page loads the
setupData
would be called with the literal['hello', 'world']
.哦。我想我应该先测试一下。我正在使用 dojo,这可以正常工作:
PHP:
Javascript:
[羊皮笑]
Oh. I guess I should have tested first. I'm using dojo, and this Just Works:
PHP:
Javascript:
[sheepish grin]