首页 > 解决方案 > XMLHttpRequest 不接受表单数据中的 json.stingify

问题描述

我想知道我做错了什么,这适用于带有 formData 和 request 的浏览器,但它不适用于节点。我尝试运行以下代码并得到我将在下面提到的错误。

 const HOST = 'http://myURL';
  const request = new XMLHttpRequest();
  const form = new FormData();
  const data = {};

  // build call to strapi
  data['CustomFields'] = {  "goes": "onOwn" };

  form.append('data', JSON.stringify(data));

  request.open('POST', `${HOST}/templates`);

  request.setRequestHeader('authorization','Bearer tokenhere');

  request.onload = function () {
      if (request.readyState === request.DONE) {
          if (request.status === 200) {
              console.log(request.response);
              console.log(request.responseText);
          }
      }
  };

  request.send(form);

我得到的错误是:

TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of FormData

在 form.append('data'... 我用 JSON.stringify 制作那个 JSON。

知道我做错了什么吗?

标签: xmlhttprequest

解决方案


推荐阅读