简体   繁体   English

JSON 格式的 POST 数据

[英]POST data in JSON format

I have some data that I need to convert to JSON format and then POST it with a JavaScript function.我有一些数据需要转换为 JSON 格式,然后使用 JavaScript 函数将其发布。

<body onload="javascript:document.myform.submit()">
<form action="https://www.test.net/Services/RegistrationService.svc/InviteNewContact" method="post" name="myform">
  <input name="firstName" value="harry" />
  <input name="lastName" value="tester" />
  <input name="toEmail" value="testtest@test.com" />
</form>
</body>

This is the way the post looks now.这就是帖子现在的样子。 I need it submit the values in JSON format and do the POST with JavaScript.我需要它以 JSON 格式提交值并使用 JavaScript 执行 POST。

Not sure if you want jQuery.不确定你是否想要 jQuery。

var form;

form.onsubmit = function (e) {
  // stop the regular form submission
  e.preventDefault();

  // collect the form data while iterating over the inputs
  var data = {};
  for (var i = 0, ii = form.length; i < ii; ++i) {
    var input = form[i];
    if (input.name) {
      data[input.name] = input.value;
    }
  }

  // construct an HTTP request
  var xhr = new XMLHttpRequest();
  xhr.open(form.method, form.action, true);
  xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

  // send the collected data as JSON
  xhr.send(JSON.stringify(data));

  xhr.onloadend = function () {
    // done
  };
};

Here is an example using jQuery ...这是一个使用jQuery的例子......

 <head>
   <title>Test</title>
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
   <script type="text/javascript" src="http://www.json.org/json2.js"></script>
   <script type="text/javascript">
     $(function() {
       var frm = $(document.myform);
       var dat = JSON.stringify(frm.serializeArray());

       alert("I am about to POST this:\n\n" + dat);

       $.post(
         frm.attr("action"),
         dat,
         function(data) {
           alert("Response: " + data);
         }
       );
     });
   </script>
</head>

The jQuery serializeArray function creates a Javascript object with the form values. jQuery serializeArray函数使用表单值创建一个 Javascript 对象。 Then you can use JSON.stringify to convert that into a string, if needed.然后,如果需要,您可以使用JSON.stringify将其转换为字符串。 And you can remove your body onload, too.你也可以移除你的身体。

I have some data that I need to convert to JSON format and then POST it with a JavaScript function.我有一些数据需要转换为 JSON 格式,然后使用 JavaScript 函数将其发布。

<body onload="javascript:document.myform.submit()">
<form action="https://www.test.net/Services/RegistrationService.svc/InviteNewContact" method="post" name="myform">
  <input name="firstName" value="harry" />
  <input name="lastName" value="tester" />
  <input name="toEmail" value="testtest@test.com" />
</form>
</body>

This is the way the post looks now.这就是帖子现在的样子。 I need it submit the values in JSON format and do the POST with JavaScript.我需要它以 JSON 格式提交值并使用 JavaScript 执行 POST。

Using the new FormData object (and other ES6 stuff), you can do this to turn your entire form into JSON:使用新的FormData对象(和其他 ES6 对象),您可以这样做以将整个表单转换为 JSON:

let data = {};
let formdata = new FormData(theform);
for (let tuple of formdata.entries()) data[tuple[0]] = tuple[1];

and then just xhr.send(JSON.stringify(data));然后只是xhr.send(JSON.stringify(data)); like in Jan's original answer.就像 Jan 的原始回答一样。

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

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