简体   繁体   English

如何发布带有隐藏表单html的JSON?

[英]How to post JSON with hidden form html?

I'm trying to post JSON object in a html hidden form. 我正在尝试以html隐藏形式发布JSON对象。 I prepared the model object in backend and am able to get it as a pretty complicated JSON object on frontend which later is stringified. 我在后端准备了模型对象,并能够将其作为前端上的一个相当复杂的JSON对象来获取,该对象随后被字符串化。 Now I need to post it. 现在,我需要发布它。 The problem is I'm not sure how convert it to form without of course looping over all attributes. 问题是我不确定如何在没有循环所有属性的情况下将其转换为表单。

Basically I have now in my JavaScript a string like: 基本上,我现在在我的JavaScript中有一个字符串,如:

str = {"language":"en","country":"GB","objectId":24639, ...}

I tried to use 'data' parameter like: 我尝试使用“数据”参数,例如:

<form enctype='application/json' action=someUrl method="post" target="_blank" name="pdfSheetForm" id="pdfSheetForm" accept-charset="utf-8">
    <input type="hidden" value="${str}"  name="data">
</form>

Did not work. 不工作。 I'm not sure what is the problem. 我不确定是什么问题。 The goal is to post JSON exactly like str looks like, not to have sth like data : "{"language": ...}" 我们的目标是发布与str完全一样的JSON,而不要发布类似data的数据:“ {” language“:...}”

You can use var str = JSON.stringify(your_object) to make your JSON object to a simple string and on your backend you can parse that string to a JSON object again when received. 您可以使用var str = JSON.stringify(your_object)使JSON对象成为一个简单的字符串,并且在后端,您可以在接收到该字符串时再次将其解析为JSON对象。

Then use pdfSheetForm.data.value = str to add the object data to your form as a string. 然后使用pdfSheetForm.data.value = str将对象数据作为字符串添加到表单中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM