我在express
中有带路由路径的服务器。响应路径之一(author/add
)将html文件发送到浏览器。使用XMLHttpRequest
时,浏览器无法加载该文件。为什么?
当我通过浏览器地址栏引用\author\add
路径时,一切正常。我得到该文件,其类型为document
(在Chrome开发工具中),然后浏览器加载该文件。当我通过XMLHttpRequest
引用该路径时,我得到了该文件,但其类型为xhr
,浏览器未显示该文件。
/author/add
路径的路由:
addAuthorRoute.route("/")
.get( function(req, res) {
console.log("Add author form request");
res.render("author.add.pug");
})
该路径的Http请求
var xhr = new XMLHttpRequest();
xhr.open("GET", "/author/add");
xhr.responseType = "document";
xhr.send();
xhr.addEventListener("load", function(event) {
console.log(event.target.response);
})
实际上在event.target.response
下,我具有该文件DOM。我是否必须使用appendChild
注入?我宁愿从服务器接收到文件后,通过浏览器显示该文件,就像在地址栏中输入author/add
一样。