我已经看到一些关于此错误的问题,而我的问题有不同的解决方案。也许有人可以解释这个?我知道我需要将 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)都被认为是一个节点
:
- >
Document
- >
Element
- >
Attr
- >
CharacterData
- >
ProcessingInstruction
- >
DocumentFragment
- >
DocumentType
- >
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/