将javascript对象转换为字符串的php代码

发布于 2024-10-31 04:42:23 字数 126 浏览 1 评论 0原文

我有 php 文件,我试图从中运行 firefox 并获得 yslow 结果。但 yslow 结果以对象形式发送,例如 [object Object] 结果使用 post 方法发送。 如何在php代码中将其转换为字符串并显示到网页上???

I have php file from which i am trying to run firefox and getting yslow results. But yslow results are sent in object form some thing like [object Object]
results are sent using post method.
how can convert it into string in php code and display to web page???

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

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

发布评论

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

评论(4

岁月流歌 2024-11-07 04:42:23

你的问题有点含糊。听起来您正在尝试发布一个正在转换为字符串的 JavaScript 对象。

{foo:'bar'}.toString(); // return "[object Object]"

如果要将对象包含在 POST 数据中,则需要序列化该对象。

Your question is a bit ambigues. it sounds like you are trying to post a javascript object which is getting converted to a string.

{foo:'bar'}.toString(); // return "[object Object]"

You need to serialize the object if you want to include it in POST data.

失退 2024-11-07 04:42:23

如果您的对象被传递给 JSON.stringify,那么它应该作为字符串返回。

var a = { "ynumreq": { "score":85} }; // currently an [object Object]
var b = JSON.stringify(a, null); // now "b" is a string representation of "a", "{ "ynumreq": { "score":85} }"

如果 b 本质上是您发送到服务器的内容,就像您提到的发布位置一样,如果您对 PHP 使用 json_decode (如建议的那样),它应该可以工作。

print_r(json_decode($_POST['string_object'])); // in this case it's '{"ynumreq":{"score":85}}'

你应该得到

{"ynumreq":{"score":85}}stdClass Object
(
    [ynumreq] => stdClass Object
    (
        [score] => 85
    )
)

从那里你可以开始获取值。这是我根据您描述数据的方式进行测试的结果。

If your object gets passed to JSON.stringify then is should be returned as string.

var a = { "ynumreq": { "score":85} }; // currently an [object Object]
var b = JSON.stringify(a, null); // now "b" is a string representation of "a", "{ "ynumreq": { "score":85} }"

If b is essentially what you're sending to the server like you mentioned where it is being POSTed, if you use json_decode to your PHP (as suggested), it should work.

print_r(json_decode($_POST['string_object'])); // in this case it's '{"ynumreq":{"score":85}}'

You should get

{"ynumreq":{"score":85}}stdClass Object
(
    [ynumreq] => stdClass Object
    (
        [score] => 85
    )
)

From there you can start getting values. This is from what I tested based on how you described your data.

时光病人 2024-11-07 04:42:23

我假设它以 JSON 形式发送,您可以使用 json_decode 函数 -
http://php.net/manual/en/function.json-decode.php

I'm assuming it's sent as JSON which you can use the json_decode function -
http://php.net/manual/en/function.json-decode.php

寄意 2024-11-07 04:42:23

JavaScript 对象字符串和 JSON 之间的区别在于,对于 JSON,变量名 foo 用双引号括起来。 json_decode 不适用于 JavaScript 对象字符串。

The difference between JavaScript object string and JSON is that with JSON the variable name foo is enclosed in double quotes. json_decode doesn't work with JavaScript object strings.

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