如何从 xhr 请求重定向
How to redirect from an xhr request
当我向我的服务器发送 xhr post 请求时。它以 302 重定向回复,但我只能记录整个重定向 html,无法让浏览器重定向到新的 url.
server.js
const Hapi = require('hapi');
const Inert = require('inert');
const server = new Hapi.Server();
const port = 8888;
server.connection({ port });
server.register([ Inert ], () => {
server.route([
{
method: 'get',
path: '/',
handler: (request, reply) => {
reply.file('index.html');
}
},
{
method: 'get',
path: '/login',
handler: (request, reply) => {
reply.file('login.html');
}
},
{
method: 'post',
path: '/login',
handler: (request, reply) => {
reply.redirect('/');
}
}
]);
server.start(() => {
console.log('Server running on ', server.info.uri);
});
});
index.html
<!doctype html>
<html>
<body>
<h1> Home </h1>
<a href="/login"> go to login </a>
</body>
</html>
login.html
<!doctype html>
<html>
<body>
<button id="home">home</button>
<script>
function goHome () {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4 && xhr.status === 200) {
console.log('Response: ', xhr.response);
}
}
xhr.open('post', '/login');
xhr.send();
}
document.getElementById('home').addEventListener('click', goHome);
</script>
</body>
</html>
有没有办法在不在客户端执行的情况下重定向到“/”?
在此先感谢您的帮助
Is there a way to redirect to '/' without doing it client side?
没有
重定向意味着“您可以在这里找到您要的内容”,而不是“将浏览器导航到此 URL”。如果浏览器要导航到它最初要求的 URL,它只会导航到重定向的 URL。
由 XMLHttpRequest 发起的请求将得到一个由 XMLHttpRequest 处理的响应。如果该响应是重定向,则将遵循它并且对新请求的响应将由 XMLHttpRequest 处理。
Ajax是在不离开页面的情况下从JS发出HTTP请求的行为。
如果您想离开页面,请不要使用Ajax。
正在寻找类似的东西,最终设置 header 然后在客户端使用它进行重定向:
$(document).on 'ajax:success', selector, (e, data, status, xhr) =>
redirectToPath = xhr.getResponseHeader('Location')
window.location = redirectToPath if redirectToPath
无法给出 Node.js 示例,但 Rails 控制器方法示例中的基本 Ruby 仅设置 header
和 head
响应:
def create
response.headers['Location'] = polymorphic_path(@document, action: :edit)
head(:ok)
end
当我向我的服务器发送 xhr post 请求时。它以 302 重定向回复,但我只能记录整个重定向 html,无法让浏览器重定向到新的 url.
server.js
const Hapi = require('hapi');
const Inert = require('inert');
const server = new Hapi.Server();
const port = 8888;
server.connection({ port });
server.register([ Inert ], () => {
server.route([
{
method: 'get',
path: '/',
handler: (request, reply) => {
reply.file('index.html');
}
},
{
method: 'get',
path: '/login',
handler: (request, reply) => {
reply.file('login.html');
}
},
{
method: 'post',
path: '/login',
handler: (request, reply) => {
reply.redirect('/');
}
}
]);
server.start(() => {
console.log('Server running on ', server.info.uri);
});
});
index.html
<!doctype html>
<html>
<body>
<h1> Home </h1>
<a href="/login"> go to login </a>
</body>
</html>
login.html
<!doctype html>
<html>
<body>
<button id="home">home</button>
<script>
function goHome () {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4 && xhr.status === 200) {
console.log('Response: ', xhr.response);
}
}
xhr.open('post', '/login');
xhr.send();
}
document.getElementById('home').addEventListener('click', goHome);
</script>
</body>
</html>
有没有办法在不在客户端执行的情况下重定向到“/”?
在此先感谢您的帮助
Is there a way to redirect to '/' without doing it client side?
没有
重定向意味着“您可以在这里找到您要的内容”,而不是“将浏览器导航到此 URL”。如果浏览器要导航到它最初要求的 URL,它只会导航到重定向的 URL。
由 XMLHttpRequest 发起的请求将得到一个由 XMLHttpRequest 处理的响应。如果该响应是重定向,则将遵循它并且对新请求的响应将由 XMLHttpRequest 处理。
Ajax是在不离开页面的情况下从JS发出HTTP请求的行为。
如果您想离开页面,请不要使用Ajax。
正在寻找类似的东西,最终设置 header 然后在客户端使用它进行重定向:
$(document).on 'ajax:success', selector, (e, data, status, xhr) =>
redirectToPath = xhr.getResponseHeader('Location')
window.location = redirectToPath if redirectToPath
无法给出 Node.js 示例,但 Rails 控制器方法示例中的基本 Ruby 仅设置 header
和 head
响应:
def create
response.headers['Location'] = polymorphic_path(@document, action: :edit)
head(:ok)
end