javascript - 通过post传递对象
我有一个看起来像这样的对象
var obj = { p1:正确, p2:正确, p3:假 。
我希望尝试将此对象作为发布请求的一部分传递
然而在另一端(在 php 中)我得到的是
[对象对象]
如何通过邮寄方式发送对象?
基本上我想做的是
我有一个隐藏的输入,其创建方式如下
>
当按下按钮时我有
$(#obj).val(obj);
$('form').submit();
Please no suggestions to use ajax as I must do it this way as it is to download a dynamically created file.
I have an object that looks like this
var obj = {
p1 : true,
p2 : true,
p3 : false
}
I am looking to try and pass this object as part of a post request.
however on the other end (in php) all I get is
[object Object]
How can I send an object via post?
basically what I am trying to do is
I have an input that is hidden and is created like so
<input id="obj" type="hidden" name="obj[]">
which is part of a hidden form.
when a button is pressed I have
$(#obj).val(obj);
$('form').submit();
Please no suggestions to use ajax as I must do it this way as it is to download a dynamically created file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在提交之前将对象序列化/转换为字符串。您可以使用
jQuery.param()
来实现此目的。You need to serialize/convert the object to a string before submitting it. You can use
jQuery.param()
for this.您可以考虑使用 JSON 表示法将对象发送到服务器。
如果您的页面中包含 JSON 解析器/渲染器,(它现在内置在所有现代浏览器中,并且在标准模式下的 IE8 中也内置)您可以将对象转换为保留其完整对象图的字符串。大多数服务器端语言现在都可以使用 JSON 解析(例如,在 PHP 中为json_decode
)。您可以在发送表单之前将该字符串放入隐藏的表单字段中。看起来像这样:
...你的服务器端会看到一个表单中的字符串
You might consider using JSON notation to send the object to the server.
If you include a JSON parser/renderer in your page,(it's built in on all modern browsers now, and also IE8 in standards mode) you can convert the object into a string preserving its full object graph. Most server-side languages now have JSON parsing available for them (in PHP it'sjson_decode
, for instance). You can put that string in your hidden form field before sending the form.That would look like this:
...and your server-side would see a string in the form