• <bdo id='hrPjp'></bdo><ul id='hrPjp'></ul>
    <legend id='hrPjp'><style id='hrPjp'><dir id='hrPjp'><q id='hrPjp'></q></dir></style></legend>
  • <i id='hrPjp'><tr id='hrPjp'><dt id='hrPjp'><q id='hrPjp'><span id='hrPjp'><b id='hrPjp'><form id='hrPjp'><ins id='hrPjp'></ins><ul id='hrPjp'></ul><sub id='hrPjp'></sub></form><legend id='hrPjp'></legend><bdo id='hrPjp'><pre id='hrPjp'><center id='hrPjp'></center></pre></bdo></b><th id='hrPjp'></th></span></q></dt></tr></i><div id='hrPjp'><tfoot id='hrPjp'></tfoot><dl id='hrPjp'><fieldset id='hrPjp'></fieldset></dl></div>

      <tfoot id='hrPjp'></tfoot>
    1. <small id='hrPjp'></small><noframes id='hrPjp'>

      1. 将 FormData 转换为查询字符串的更简单方法

        时间:2023-10-13

            <tbody id='m0b7X'></tbody>

          <small id='m0b7X'></small><noframes id='m0b7X'>

            <legend id='m0b7X'><style id='m0b7X'><dir id='m0b7X'><q id='m0b7X'></q></dir></style></legend>
                • <bdo id='m0b7X'></bdo><ul id='m0b7X'></ul>
                  <tfoot id='m0b7X'></tfoot>
                  <i id='m0b7X'><tr id='m0b7X'><dt id='m0b7X'><q id='m0b7X'><span id='m0b7X'><b id='m0b7X'><form id='m0b7X'><ins id='m0b7X'></ins><ul id='m0b7X'></ul><sub id='m0b7X'></sub></form><legend id='m0b7X'></legend><bdo id='m0b7X'><pre id='m0b7X'><center id='m0b7X'></center></pre></bdo></b><th id='m0b7X'></th></span></q></dt></tr></i><div id='m0b7X'><tfoot id='m0b7X'></tfoot><dl id='m0b7X'><fieldset id='m0b7X'></fieldset></dl></div>

                  本文介绍了将 FormData 转换为查询字符串的更简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在通过 XMLHttpRequest 发送一个 POST 请求,其中将数据输入到 HTML 表单中.不受 JavaScript 干扰的表单将提交其编码为 application/x-www-form-urlencoded 的数据.

                  I’m sending a POST request via XMLHttpRequest with data entered into an HTML form. The form without interference of JavaScript would submit its data encoded as application/x-www-form-urlencoded.

                  使用 XMLHttpRequest,我想通过 FormData API 发送数据,该 API 不起作用,因为它将数据视为编码为 multipart/form-data.因此,我需要将数据作为查询字符串,正确转义,写入 XMLHttpRequest 的发送方法.

                  With the XMLHttpRequest, I wanted to send the data with via the FormData API which does not work since it treats the data as if it were encoded as multipart/form-data. Therefor I need to write the data as a query string, properly escaped, into the send method of the XMLHttpRequest.

                  addEntryForm.addEventListener('submit', function(event) {
                      // Gather form data
                      var formData = new FormData(this);
                      // Array to store the stringified and encoded key-value-pairs.
                      var parameters = []
                      for (var pair of formData.entries()) {
                          parameters.push(
                              encodeURIComponent(pair[0]) + '=' +
                              encodeURIComponent(pair[1])
                          );
                      }
                  
                      var httpRequest = new XMLHttpRequest();
                      httpRequest.open(form.method, form.action);
                  
                      httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                  
                      httpRequest.onreadystatechange = function() {
                          if (httpRequest.readyState === XMLHttpRequest.DONE) {
                              if (httpRequest.status === 200) {
                                  console.log('Successfully submitted the request');
                              } else {
                                  console.log('Error while submitting the request');
                              }
                          }
                      };
                  
                      httpRequest.send(parameters.join('&'));
                  
                      // Prevent submitting the form via regular request
                      event.preventDefault();
                  });
                  

                  现在,带有 for ... of 循环等的整个事情似乎有点令人费解.有没有更简单的方法将 FormData 转换为查询字符串?或者我可以用不同的编码发送 FormData 吗?

                  Now this whole thing with the for ... of loop, etc. seems a bit convoluted. Is there a simpler way to transform FormData into a query string? Or can I somehow send FormData with a different encoding?

                  推荐答案

                  你可以使用 URLSearchParams

                  const queryString = new URLSearchParams(new FormData(myForm)).toString()
                  

                  这篇关于将 FormData 转换为查询字符串的更简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                • <tfoot id='TQjqT'></tfoot>

                    <tbody id='TQjqT'></tbody>

                      <small id='TQjqT'></small><noframes id='TQjqT'>

                      <i id='TQjqT'><tr id='TQjqT'><dt id='TQjqT'><q id='TQjqT'><span id='TQjqT'><b id='TQjqT'><form id='TQjqT'><ins id='TQjqT'></ins><ul id='TQjqT'></ul><sub id='TQjqT'></sub></form><legend id='TQjqT'></legend><bdo id='TQjqT'><pre id='TQjqT'><center id='TQjqT'></center></pre></bdo></b><th id='TQjqT'></th></span></q></dt></tr></i><div id='TQjqT'><tfoot id='TQjqT'></tfoot><dl id='TQjqT'><fieldset id='TQjqT'></fieldset></dl></div>
                        <bdo id='TQjqT'></bdo><ul id='TQjqT'></ul>
                        <legend id='TQjqT'><style id='TQjqT'><dir id='TQjqT'><q id='TQjqT'></q></dir></style></legend>