2016-10-26 2 views
0

xhr 게시 요청을 내 서버로 보낼 때. 302 리디렉션으로 응답하지만 전체 리디렉션 HTML 만 기록 할 수 있으며 새 URL로 리디렉션하도록 브라우저를 가져올 수 없습니다.xhr 요청에서 리디렉션하는 방법

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> 

그것을 클라이언트 측을하지 않고 '/'로 리디렉션하는 방법이 있나요?

도움 주셔서 감사

답변

1

에 대한 사전에 그것을 클라이언트 측을하지 않고 '/'로 리디렉션 할 수있는 방법이 있나요?

XMLHttpRequest 객체가 처리 될 응답을 얻을 것이다 XMLHttpRequest 객체에 의해 개시된 요청 호. 해당 응답이 리디렉션 인 경우이를 따르고 새 요청에 대한 응답이 XMLHttpRequest에 의해 처리됩니다.

Ajax는 JS에서 페이지를 떠나지 않고 HTTP 요청을하는 행위입니다.

페이지를 떠나려면 Ajax를 사용하지 마십시오.

관련 문제