Javascript 使用按钮并单击处理程序实现到新页面的链接
以下是处理GET请求的服务器端代码:Javascript 使用按钮并单击处理程序实现到新页面的链接,javascript,jquery,node.js,express,Javascript,Jquery,Node.js,Express,以下是处理GET请求的服务器端代码: app.get('/', function(req, res){ res.sendFile(__dirname + "/public/html/index.html"); }); app.get("/new-html", function(req, res){ console.log("Request /new-html"); res.sendFile(__dirname + "/public/html/new.html"); })
app.get('/', function(req, res){
res.sendFile(__dirname + "/public/html/index.html");
});
app.get("/new-html", function(req, res){
console.log("Request /new-html");
res.sendFile(__dirname + "/public/html/new.html");
})
这是客户端中触发请求的代码:
$("#load-html").click(function() {
$.get("/new-html")
})
加载页面并使用根url发出请求时,会呈现index.hml本身
我想要的是当我用
id=load html
按钮接收并呈现服务器返回的全新html文件时。您应该在index.html页面中使用链接标记
<a href="/new-html">some text</a>
阅读文档:您正在请求文件,但放弃了它。我已经阅读了。它没有告诉我如何呈现有效的html文件。我不想嵌套html元素。相反,我想加载一个完整的带有DOCTYPE body head和所有sh*t的html,你到底是什么意思?将收到的文档放入
?导航到它?恐怕你不明白我的问题。我正在尽力,而且我不慢。你解释得不好。您希望浏览器在何处呈现文件…?window.location.href
正是我想要的:)
$("#load-html").click(function() {
window.location.href = 'http://yourdomain.com/new-html'
})