Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ajax和express路由重定向到新url_Javascript_Ajax_Express_Ejs - Fatal编程技术网

Javascript 使用ajax和express路由重定向到新url

Javascript 使用ajax和express路由重定向到新url,javascript,ajax,express,ejs,Javascript,Ajax,Express,Ejs,我在auth.ejs页面上有一个id为“tune in”的按钮,单击该按钮应转到一个新的页面index.ejs 但是,单击该按钮时,不会呈现索引页。相反,我仍然在auth页面上,但是我确实有消息应该显示登录到控制台的tune-in主页 在html ejs页面视图文件夹>auth.ejs上 控制器文件夹>authController.js 视图文件夹>index.ejs 这只是一个普通的html页面,应该在单击时呈现 编辑: index.js 如果我没有错,这就是你需要的 document.ge

我在auth.ejs页面上有一个id为“tune in”的按钮,单击该按钮应转到一个新的页面index.ejs

但是,单击该按钮时,不会呈现索引页。相反,我仍然在auth页面上,但是我确实有消息应该显示登录到控制台的tune-in主页

在html ejs页面视图文件夹>auth.ejs上

控制器文件夹>authController.js

视图文件夹>index.ejs 这只是一个普通的html页面,应该在单击时呈现

编辑: index.js


如果我没有错,这就是你需要的

 document.getElementById('tune-in').addEventListener('click', function() {
$.ajax({
  type: 'GET',
  url: '/tune-in',
  success: function(data){
    console.log("should display tune-in home page");
    // data must be valid html
    $("html").html(data); // this line tells to load the data
    // $("#otherid").html(data);
   }
 });
}, false);

我可以为您添加app.js或任何主文件吗?您还可以添加处理路由的位置吗?我想看看您是否正确设置了模板,以及是否正确设置了路由。这种行为确实是预期的,因为您不要求呈现页面。$。ajax只是获取索引的内容。除非您告诉它,否则它不会呈现内容。@AnamulHasan,但我告诉它在authController.js中呈现您想呈现index的内容而不是auth.ejs的内容。“我说得对吗?”阿纳穆哈桑说得对!我认为你收到的内容不是有效的html。所以它没有正确加载,而是显示空页面。不,我是无意中重新加载了auth.ejs。几分钟前,我从索引改成了索引。你的回答很好,谢谢!
  app.get('/tune-in', function(req, res){
    res.render('index');
  });
var express = require('express');
var cookieParser = require('cookie-parser');

// userController takes care of rendering the view and
// routing requests to the server
var authController = require('./controllers/authController');
var userController = require('./controllers/userController');

var app = express();

app.set('view engine', 'ejs');
app.use(express.static('./public'))
   .use(cookieParser());

authController(app);

app.listen(process.env.PORT || 4000);
console.log("Listening to port 4000...");
 document.getElementById('tune-in').addEventListener('click', function() {
$.ajax({
  type: 'GET',
  url: '/tune-in',
  success: function(data){
    console.log("should display tune-in home page");
    // data must be valid html
    $("html").html(data); // this line tells to load the data
    // $("#otherid").html(data);
   }
 });
}, false);