如何通过http请求呈现模板

时间:2019-07-25 18:37:37

标签: express routing xmlhttprequest

我在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一样。

0 个答案:

没有答案