javascript - 通过邮件传递对象 [英] javascript - pass object via post
问题描述
我有一个看起来像这样的对象
I have an object that looks like this
var obj = {
p1:true,
p2:true,
p3:false
}
var obj = { p1 : true, p2 : true, p3 : false }
我希望尝试将此对象作为帖子请求的一部分传递。
I am looking to try and pass this object as part of a post request.
然而另一端(在php中)我得到的是
however on the other end (in php) all I get is
[object Object]
[object Object]
如何通过邮寄方式发送对象?
How can I send an object via post?
基本上我想做的是
我有一个隐藏的输入并且是这样创建的
I have an input that is hidden and is created like so
< input id =objtype =hiddenname =obj []>
这是隐藏表单的一部分。
which is part of a hidden form.
按下按钮时我有
$(#obj).val(obj);
$('form').submit();
请不要使用ajax的建议,因为我必须这样做因为它是下载动态创建的文件。
Please no suggestions to use ajax as I must do it this way as it is to download a dynamically created file.
推荐答案
您需要在提交之前将对象序列化/转换为字符串。您可以使用 jQuery.param()
为此。
You need to serialize/convert the object to a string before submitting it. You can use jQuery.param()
for this.
$('#obj').val(jQuery.param(obj));
这篇关于javascript - 通过邮件传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!