Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.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 我应该在XMLHttpRequest中使用什么回调函数来呈现响应?_Javascript_Ajax_Node.js_Express - Fatal编程技术网

Javascript 我应该在XMLHttpRequest中使用什么回调函数来呈现响应?

Javascript 我应该在XMLHttpRequest中使用什么回调函数来呈现响应?,javascript,ajax,node.js,express,Javascript,Ajax,Node.js,Express,用户在页面上执行某项操作后,我使用ajax将一些数据发送到Node.js Express服务器。然后,该服务器执行以下操作: router.post('/', function(req, res){ //user authentication then, inside that callback res.redirect('/dashboard'); } router.get('/', function(req, res, next) { console.log("dashboar

用户在页面上执行某项操作后,我使用ajax将一些数据发送到Node.js Express服务器。然后,该服务器执行以下操作:

router.post('/', function(req, res){
//user authentication then, inside that callback
  res.redirect('/dashboard');
}
router.get('/', function(req, res, next) {
    console.log("dashboard"); //<--This code is running
    res.render('index',{title:"You are logged in"});
}
onclick='httpGetAsync("/createUser", console.log)'
dashboard.js然后执行以下操作:

router.post('/', function(req, res){
//user authentication then, inside that callback
  res.redirect('/dashboard');
}
router.get('/', function(req, res, next) {
    console.log("dashboard"); //<--This code is running
    res.render('index',{title:"You are logged in"});
}
onclick='httpGetAsync("/createUser", console.log)'
index.html会登录到我的浏览器控制台,而不是查看我的“您登录了”页面。这显然是因为我的onclick侦听器执行以下操作:

router.post('/', function(req, res){
//user authentication then, inside that callback
  res.redirect('/dashboard');
}
router.get('/', function(req, res, next) {
    console.log("dashboard"); //<--This code is running
    res.render('index',{title:"You are logged in"});
}
onclick='httpGetAsync("/createUser", console.log)'
这里的console.log显然是错误的,我应该调用什么

如何在dashboard.js中获取res.render'index'{title:您已登录}以实际渲染

这很像,但这个问题不包括包含前端代码的部分,我认为这就是问题所在。最后一句话是:不,我从来没想过。我最终以不同的方式构建了我的应用程序,在模式中使用登录部分,而不是另一个页面。。。非常令人沮丧。

因为XHR是用来发送和恢复响应的,所以您将收到状态为301或302的html页面作为响应,可能的解决方案是将重定向url作为响应发送

router.post('/', function(req, res){
  //user authentication then, inside that callback
  res.send('/dashboard');
}
收到此响应后,您可以在回调时重定向到响应url

function redirect(res){
  window.location = res;
}

onclick='httpGetAsync/createUser,redirect'

我是否需要编写一些replaceDocumentWith函数?是的,您的服务器应该只返回需要更改的元素,然后使用res=>{document.getElementByIdresponseArea.innerHTML=res;}之类的内容作为回调,而不是console.log.document.write?无论如何,它都不能异步工作。似乎我想这样做:这似乎很有希望,但似乎没有定义重定向。您是否定义了重定向函数@client?我应该注意到这一点。谢谢