如何发送表格' POST' JSON结构?

时间:2014-12-16 17:15:29

标签: html json forms

我需要html在POST请求中发送这样的结构:

[{"applicationUUID":"a61a0a0a-c2ff-4238-ab0e-69ae8898a00b","applicationName":"SSM - Incident Management","hasSDF":true,"hasSLA":true},{"applicationUUID":"9c3e7a8a-dace-469d-a275-c804dfa44a41","applicationName":"SSM - Change Management","hasSDF":false,"hasSLA":true},{"applicationUUID":"fcd84a6b-73fe-453a-965a-c841ae6df170","applicationName":"SSM - Problem Management","hasSDF":false,"hasSLA":true}]

如何像变量字符串一样发送?

2 个答案:

答案 0 :(得分:0)

简短的回答是“你不能”。用于发出POST请求的唯一HTML机制是一个表单,它支持three encoding methods; JSON不是其中之一。已经提出了JSON Forms的规范,但浏览器支持很弱(可能不存在),并且数据结构与您想要的数据结构不匹配。

从浏览器发送数据的唯一方法是使用JavaScript。您需要收集数据,创建数组和对象,convert them to JSON,然后make the request using XHR,同时确保set the correct content-type

答案 1 :(得分:0)

给定的数据formData是JSON。您可以使用AJAX发送JSON数据。

var formData = [
{
     "applicationUUID":"a61a0a0a-c2ff-4238-ab0e-69ae8898a00b",
     "applicationName":"SSM - Incident Management",
     "hasSDF":true,
     "hasSLA":true
},
{
     "applicationUUID":"9c3e7a8a-dace-469d-a275-c804dfa44a41",
     "applicationName":"SSM - Change Management",
     "hasSDF":false,
     "hasSLA":true
},
{
     "applicationUUID":"fcd84a6b-73fe-453a-965a-c841ae6df170",
     "applicationName":"SSM - Problem Management",
     "hasSDF":false,
     "hasSLA":true
}];

$.ajax({
  type: 'POST',
  url: 'http://example.com:6060/testAjax/', // Target URL
  contentType: 'application/json',
  dataType: 'json',
  data: JSON.stringify(formData)
});