当服务器可以走两条不同的路线时如何获取客户端?
How to fetch client side when the server can go two different routes?
我不确定如何执行此操作,但我有一个 post 请求服务器端根据查询结果转到两个不同的路由。
app.post('/check', function(req, res) {
connection.query("select exists (select * from visit WHERE id = ?) as 'res' ",
id, function(err, result) {
if (err) throw err;
if(result[0].res == 0) {
console.log("Doesn't exist in db.")
res.redirect(307, '/post'); //goes to /post but after post it doesn't go to db
}
else {
//if data exists update
console.log("Exists in db.")
res.redirect(307, '/up'); //goes to up but doesn't go to up.html afterwards
}
});
所以上面的请求有效。下面它不改变网络浏览器的页面。
app.post('/post', function(req, res) {
//connects to mysql to update database
res.redirect('/post.html'); //---when database updates the page doesn't change
});
下面也不行:
app.post('/up', async function(req, res) {
console.log("/qrupdate: post timeOut");
let now = moment().format('MMMM Do YYYY, h:mm:ss a');
data = req.body;
let id = data.id.toString();
connection.query("UPDATE visit SET timeOut = ? WHERE id=?", [now, id],
function(err, result) {
if (err) throw err;
});
res.redirect('/up.html'); //doesn't change page
});
那么为什么网页重定向了却没有变化呢?
另外,如果我需要做一个获取客户端,获取应该是什么样子的?
我不知道它是走那条路还是那条路,我能行吗?如果post请求是这个,更新指定位置的页面,否则更新这个位置的页面?我不知道什么是可能的或者它应该是什么样子。
fetch(api_url + '/qrcheck', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json'
},
body: qrdata
})
.then((response) => response.json())
.then((data) => {
console.log('Success:', data);
//So here it prints the requests but I guess I wanted to do some kind of if statement
//like if post request was /post then window.location = post.html. Else window.location = up.html
})
.catch((error) => {
console.error('Error:', error);
});
所以我仍然不知道为什么它不更改页面,如果它发布到 /check 然后 /up,它不会转到 up.html 但如果它没有首先通过 /check (我假设它与它重定向两次有关,如果有人可以提供更多信息,这将有助于人们知道),但你可以在你的 fetch 中有一个 if 语句使用 data
.
fetch(api_url + '/check', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json'
},
body: qrdata
})
.then((data) => {
console.log('Success:', data);
//I have added two if statements to check the location
//then the client side is updated to that page
if (data.url == url + '/up.html') {
window.location.replace(url + '/up.html');
}
if (data.url == url + '/post.html') {
window.location.replace(url + '/post.html');
}
})
.catch((error) => {
console.error('Error:', error);
});
我不确定如何执行此操作,但我有一个 post 请求服务器端根据查询结果转到两个不同的路由。
app.post('/check', function(req, res) {
connection.query("select exists (select * from visit WHERE id = ?) as 'res' ",
id, function(err, result) {
if (err) throw err;
if(result[0].res == 0) {
console.log("Doesn't exist in db.")
res.redirect(307, '/post'); //goes to /post but after post it doesn't go to db
}
else {
//if data exists update
console.log("Exists in db.")
res.redirect(307, '/up'); //goes to up but doesn't go to up.html afterwards
}
});
所以上面的请求有效。下面它不改变网络浏览器的页面。
app.post('/post', function(req, res) {
//connects to mysql to update database
res.redirect('/post.html'); //---when database updates the page doesn't change
});
下面也不行:
app.post('/up', async function(req, res) {
console.log("/qrupdate: post timeOut");
let now = moment().format('MMMM Do YYYY, h:mm:ss a');
data = req.body;
let id = data.id.toString();
connection.query("UPDATE visit SET timeOut = ? WHERE id=?", [now, id],
function(err, result) {
if (err) throw err;
});
res.redirect('/up.html'); //doesn't change page
});
那么为什么网页重定向了却没有变化呢? 另外,如果我需要做一个获取客户端,获取应该是什么样子的? 我不知道它是走那条路还是那条路,我能行吗?如果post请求是这个,更新指定位置的页面,否则更新这个位置的页面?我不知道什么是可能的或者它应该是什么样子。
fetch(api_url + '/qrcheck', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json'
},
body: qrdata
})
.then((response) => response.json())
.then((data) => {
console.log('Success:', data);
//So here it prints the requests but I guess I wanted to do some kind of if statement
//like if post request was /post then window.location = post.html. Else window.location = up.html
})
.catch((error) => {
console.error('Error:', error);
});
所以我仍然不知道为什么它不更改页面,如果它发布到 /check 然后 /up,它不会转到 up.html 但如果它没有首先通过 /check (我假设它与它重定向两次有关,如果有人可以提供更多信息,这将有助于人们知道),但你可以在你的 fetch 中有一个 if 语句使用 data
.
fetch(api_url + '/check', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json'
},
body: qrdata
})
.then((data) => {
console.log('Success:', data);
//I have added two if statements to check the location
//then the client side is updated to that page
if (data.url == url + '/up.html') {
window.location.replace(url + '/up.html');
}
if (data.url == url + '/post.html') {
window.location.replace(url + '/post.html');
}
})
.catch((error) => {
console.error('Error:', error);
});