gpt4 book ai didi

javascript - 单击事件后重定向 url 并更改页面,并使用 GET 将 html 文件提供给浏览器

转载 作者:行者123 更新时间:2023-11-28 05:10:10 24 4
gpt4 key购买 nike

在用户单击主页中的按钮并使用 get 方法显示 list.html 后,我尝试将 localhost:9000 重定向到 localhost:9000/list 。如何使用 display.js 中的 AJAX 调用和 paths.js 中的 get 请求来重定向 url 并提供 list.html。当我点击按钮时,什么也没有发生。为了更改 url,我尝试了 windows.location 和 location.href,但我不确定是否需要这些。

目录布局

dir main
-server.js
dir subMain
dir display
-display.js
dir routing
-routes.js
dir public
-home.html
-list.html

服务器.js

var path = require('path');
var express = require('express');
var app = express();
require('./subMain/routing/routes.js')(app, path, express);

app.listen(9000, function(){
console.log('connected on 9000')
})

routes.js

module.exports = function(app, path, express){
app.use(express.static("app"))
app.use(express.static(__dirname + "/public"));
app.use(express.static(__dirname + "/routing"));
app.use(express.static(__dirname + "/display"));
app.use(function(request, response, next){
response.sendFile(path.join(__dirname + "/..", "public", "home.html"));
})
app.get('/list', function(request, response){
response.sendFile(path.join(__dirname + '/..', 'public', 'list.html'));
});

}

显示.js

$(document).on('click', '#btn', sendSurvery);

function sendSurvery(){
window.location = 'survey.html';
//var myQueryUrl = "http://localhost:9000/survey";

//$.ajax({url: myQueryUrl, method: 'GET', success: function(result){
// location.href = "http://localhost:9000/list"

//}}).done(function(response){

//});
}

更新:由于 AJAX 不需要更改浏览器上的 html,因此哪里是使用 windows.location 的合适位置,以及如何将其连接到对 list.html 的 get 请求?现在,我尝试在 server.js 文件和routes.js 文件中执行 get 请求,但是当我单击按钮时,网址将更改为调查,但不会更改浏览器上的内容(list.html 不显示)

更新 2:我摆脱了 AJAX 调用,只使用:windows.location = localhost:9000/list 但 get 请求并未将 list.html 文件发送到浏览器。我也尝试将 get 请求放入 server.js 文件中,但它仍然不会执行任何操作

最佳答案

这里不需要 AJAX(A 代表异步) - 您只需执行 window.location = http://localhost:9000/survey这将有效地告诉浏览器向服务器发送同步 GET 请求,并用它收到的响应替换当前页面。

关于javascript - 单击事件后重定向 url 并更改页面,并使用 GET 将 html 文件提供给浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41440307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com