从前端页面使用 'fetch' 进行重定向时出现问题
Problem with redirect using 'fetch' from frontend page
我在网页中使用以下 JS 在网页中的图像 'clicking' 上将信息发送到 Node.js 服务器,我遇到了一次 'redirect' 的问题'fetch' 被执行:
fetch('/members/pages/callup', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({name: splits[1], presence: available, str: 'Some string: &=&'})
})
.then(function(res) {res.json()})
.then(function(res) {
if(res.response) {
redirect: window.location.replace("/members/pages/" + splits[1]);
} else {
alert("Error in the response");
}
})
.catch(function(err) {
alert("Error in the fetch call..." + err);
})
提取似乎正确地将 'body' 数据发送到服务器。但是我收到以下错误:"Error in the fetch call...TypeError: Cannot read property 'response' of undefined"...
服务器使用前端发送的信息执行数据库调用,我以为我需要做的就是发送回“200(OK)”响应...这是服务器代码:
app.post('/member/pages/callup', jsonParser, function (req, res) {
console.log("I RECEIVED FROM CLIENT THE FOLLOWING:");
console.log(req.body); //works fine, prints output from frontend 'fetch' to console...
db.lookupMember(req.body.name)
.then(function(foundUser) {
console.log('Async success!', foundUser); //works fine, prints database info to console...
if (typeof foundUser != "undefined") {
res.sendStatus(200); //trying this to 'reply' back to 'fetch' in frontend...is this not correct?
} //'foundUser' is NOT'undefined'...
})
.catch(function(error) {
console.log('UNABLE TO RETRIEVE MEMBER INFORMATION FROM THE DATABASE...' + error);
res.redirect('/'); //route to splash page...
});
})
任何建议表示赞赏,这已经从一个小问题变成了一个大问题。我先谢谢你了。
代码中几乎没有问题。如果修复,代码应该可以正常工作。
- 你
forgot to return res.json() from the function
在一个地方。使它成为 return res.json()
并且它会正常工作(Inside fetch,然后是第一个)。由于未返回,res 未定义,导致错误
- 您正在尝试使用
res.response
,但 res 并未作为正确的 json 从节点服务器发送。这将在 res.json() 处失败。你应该做类似 res.send({response: true})
的事情
- 服务器中的 if 循环后出现语法错误。它需要
redirect =
而不是 redirect:
。也没有在任何地方声明重定向。 (注意:您可能不需要此处的重定向变量,只需 window.lo... 也应该有效)
注:与OP讨论后更新了原答案
我在网页中使用以下 JS 在网页中的图像 'clicking' 上将信息发送到 Node.js 服务器,我遇到了一次 'redirect' 的问题'fetch' 被执行:
fetch('/members/pages/callup', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({name: splits[1], presence: available, str: 'Some string: &=&'})
})
.then(function(res) {res.json()})
.then(function(res) {
if(res.response) {
redirect: window.location.replace("/members/pages/" + splits[1]);
} else {
alert("Error in the response");
}
})
.catch(function(err) {
alert("Error in the fetch call..." + err);
})
提取似乎正确地将 'body' 数据发送到服务器。但是我收到以下错误:"Error in the fetch call...TypeError: Cannot read property 'response' of undefined"...
服务器使用前端发送的信息执行数据库调用,我以为我需要做的就是发送回“200(OK)”响应...这是服务器代码:
app.post('/member/pages/callup', jsonParser, function (req, res) {
console.log("I RECEIVED FROM CLIENT THE FOLLOWING:");
console.log(req.body); //works fine, prints output from frontend 'fetch' to console...
db.lookupMember(req.body.name)
.then(function(foundUser) {
console.log('Async success!', foundUser); //works fine, prints database info to console...
if (typeof foundUser != "undefined") {
res.sendStatus(200); //trying this to 'reply' back to 'fetch' in frontend...is this not correct?
} //'foundUser' is NOT'undefined'...
})
.catch(function(error) {
console.log('UNABLE TO RETRIEVE MEMBER INFORMATION FROM THE DATABASE...' + error);
res.redirect('/'); //route to splash page...
});
})
任何建议表示赞赏,这已经从一个小问题变成了一个大问题。我先谢谢你了。
代码中几乎没有问题。如果修复,代码应该可以正常工作。
- 你
forgot to return res.json() from the function
在一个地方。使它成为return res.json()
并且它会正常工作(Inside fetch,然后是第一个)。由于未返回,res 未定义,导致错误 - 您正在尝试使用
res.response
,但 res 并未作为正确的 json 从节点服务器发送。这将在 res.json() 处失败。你应该做类似res.send({response: true})
的事情
- 服务器中的 if 循环后出现语法错误。它需要
redirect =
而不是redirect:
。也没有在任何地方声明重定向。 (注意:您可能不需要此处的重定向变量,只需 window.lo... 也应该有效)
注:与OP讨论后更新了原答案