javascript - 了解错误 : Uncaught TypeError: Failed to execute 'appendChild' on 'Node' : parameter 1 is not of type 'Node'

标签 javascript

我已经看到一些关于此错误的问题,而我的问题有不同的解决方案。也许有人可以解释这个?我知道我需要将 JSON 响应转换为字符串,但在这方面错误消息对我来说毫无意义。

var root = "someurl";
url = root + url;
$.ajax({
   type: 'POST',
   url: url,
   data: formData,
}).done(function (response) {
   // Fails with error: Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
   document.getElementById("resultBox").appendChild(response);

   // OK
   document.getElementById("resultBox").textContent = JSON.stringify(response, null, 4);
}

最佳答案

.appendChild

[.appendChild] 函数需要第一个参数作为 Node .

以下任何一个接口(interface)都被认为是一个节点:

  1. > Document
  2. > Element
  3. > Attr
  4. > CharacterData
  5. > ProcessingInstruction
  6. > DocumentFragment
  7. > DocumentType
  8. > Notation

任何不属于上述内容的都不会被视为节点。因此,当您传递对象或字符串(响应)时,会引发以下错误:

TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

ajax 请求返回的response 很可能是Object .

JSON.stringify 会将 Object 转换为 String .

两者都不是列出的接口(interface)之一。

关于javascript - 了解错误 : Uncaught TypeError: Failed to execute 'appendChild' on 'Node' : parameter 1 is not of type 'Node' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56033392/

相关文章:

javascript - 如何使 <textarea> 中的空白行为与 HTML 预览相匹配?

javascript - 依赖 Content-Type : text/plain to mitigate malicious javascript execution in response? 是否安全

javascript - JQuery/AJAX : How to pass variables to some JS-API?

javascript - 更新 MYSQL 表给出成功消息,但不更新表

javascript - AngularJS:使用指令前置 HTML

javascript - 我将如何去使用 window.find() activate on the text only once

javascript - Nuxt.js + vuetify 图像未加载

php - 如何使用 javascript 或 jquery 从 html 5 中的输入类型文件中获取文件名

javascript - 在运行时修改 WebGL 着色器所需的最少代码是什么?

Javascript 数组 - 全局范围的问题