2016-10-26 25 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を使用しないでください。

関連する問題