Невозможно перенаправить на страницу index.html после входа

Мое клиентское приложение сначала берет user_ID и пароль со страницы login.html, а затем, когда нажимается кнопка "Войти", функция "display()" запускает сохраненный идентификатор и пароль из полей ввода и отправляет их на сервер. сервер подтверждает идентификатор входа и пароль и пытается отправить страницу index.html. Но я не могу отправить страницу index.html. Может быть, я что-то упустил в моем HTML-коде или на стороне сервера.

мой HTML(загрузочный) код

          <input type="text" id="user_id" class="form-control input-lg" placeholder="User ID" >

          <input type="password" id="password_id" class="form-control input-lg" placeholder="Password" >

          <button class="btn btn-primary btn-lg btn-block" onclick="Display()" > Sign In link</button>

Функция JavaScript "Display()":

        <script>

    function Display(){
                var ID = document.getElementById("user_id").value;
                var passwrd = document.getElementById("password_id").value;
                var login = "/login:" + ID + "-" + passwrd + "--" ;

                var http = new XMLHttpRequest();
        http.open("GET", login, true); 
        http.onreadystatechange = function() {          if(http.readyState == 4 && http.status == 200) {
               var data = http.responseText;
                        if (data=="0"){
            document.getElementById("info_id").innerHTML =  "wrong password";
            } 
                        else if (data=="1"){
            document.getElementById("info_id").innerHTML =  "logging in as Admin";
            page();
            }           

        }
        }
        http.send(null); 
             }
    function page(){
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
            if (request.readyState === 4) {
            if (request.status === 200) {
                //document.body.className = 'ok';
                //console.log(request.responseText);
            } else {
                //document.body.className = 'error';
            }
            }
        };
        request.open("GET", "index.html" , true);
        request.send(null);

      }
    </script>

Код Python baseHTTPServer:

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(s):
    s.send_response(200)
    s.send_header("Content-type", "text/html")
    s.end_headers()


def do_GET(s):
    HTTP_request=s.requestline
    index_1=HTTP_request.index("GET /")
    index_2=HTTP_request.index(" HTTP/1.1")
    file_name=HTTP_request[index_1+5:index_2]
    print 'HTTP_request:',HTTP_request 
    if HTTP_request.find('login')>-1:
        print 'got', file_name  # file name is formatted as "login:anum-1234--"
        ID = file_name[6:file_name.find('-')]
        password = file_name[file_name.find('-')+1:file_name.find('--')]
        if ID == "anum" and password == "1234":
            admin = 1
            print 'admin ENTERED'  # <--working till here
            file1=open('index.html','r')
            file_read=file1.read()
            s.send_response(301)
            s.send_header('Location','http://index.html') # <- is it correct this way ?
            #s.send_header('Location',file_read)  ## <- or this way
            s.end_headers() 
        else:
            admin = 0  
            s.wfile.write("0")

Любая помощь будет высоко ценится, заранее спасибо:)

0 ответов

Другие вопросы по тегам