2013-12-12 254 views
0

我有一個簡單的登錄頁面,它將登錄信息發送到servlet,並作爲響應接收一個在jQuery中解釋的參數。

數據發送正確,轉到servlet,它也正確地設置參數(我可以看到在響應頭中的Firebug中)。

問題是我想從返回的響應中檢索數據並將其分配給JavaScript變量。 request對象是空的,而在Chrome檢查我看到一個警告:

Uncaught TypeError: Object has no method 'getResponseHeader'.

當我把它用console.log()什麼不回我,沒有值顯示。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<meta name="description" content=""> 
<meta name="author" content=""> 
<link rel="shortcut icon" href="../../assets/ico/favicon.png"> 
<link 
    href="http://minikomi.github.io/Bootstrap-Form-Builder/assets/css/lib/bootstrap.min.css" 
    rel="stylesheet"> 
<link 
    href="http://minikomi.github.io/Bootstrap-Form-Builder/assets/css/lib/bootstrap-responsive.min.css" 
    rel="stylesheet"> 
<link 
    href="http://minikomi.github.io/Bootstrap-Form-Builder/assets/css/custom.css" 
    rel="stylesheet"> 
<script src="http://code.jquery.com/jquery-2.0.3.js"></script> 
<script src="../js/jquery.validate.js"></script> 


<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Logowanie</title> 

<!-- Bootstrap core CSS --> 
<link href="../css/bootstrap.min.css" rel="stylesheet"> 

<!-- Custom styles for this template --> 
<link href="../css/signin.css" rel="stylesheet"> 

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --> 
<!--[if lt IE 9]> 
     <script src="../../assets/js/html5shiv.js"></script> 
     <script src="../../assets/js/respond.min.js"></script> 
    <![endif]--> 
</head> 

<body> 

    <div class="container"> 

     <form class="form-signin" action="Login" method="post"> 
      <h2 class="form-signin-heading">Logowanie</h2> 

      <input type="email" name="email" class="form-control" id="email" 
       placeholder="Adres email" autofocus> 


       <input type="password" id="password" 
       name="password" class="form-control" placeholder="Haslo"> 


     </form> 
     <button id="zaloguj" value="zaloguj" class="btn btn-success btn-large"> 
      <i class="icon-white icon-th-list"></i>Zaloguj 
     </button> 

    </div> 
    <!-- /container --> 


    <!-- Bootstrap core JavaScript 
    ================================================== --> 
    <!-- Placed at the end of the document so the pages load faster --> 

    <script type="text/javascript"> 

     $('#zaloguj').click(function() { 
      var email = $("#email").val(); 
      var password = $("#password").val(); 

      console.log(email+password); 
      var data = "email=" + email + "&password=" + password; 
      $.ajax({ 
       url : "Login", 
       type : "POST", 
       data : data, 

       success : function(request) { 
        console.log(request); 
        var log = request.getResponseHeader("log"); 
        console.log(log);     

        if (log == 1) { 

         $(":header").after("Poprawne logowanie").css({ background: "#ccc", color: "green" }); 
         document.location.href = '/landing.html'; 
        } else { 
         $(":header").after("Błędne logowanie").css({ background: "#ccc", color: "red" }); 
        } 
       }, 

      }); 

     }); 
    </script> 
</body> 
</html> 
+1

什麼'Login' returnin G? 'request'參數是從URL返回的數據,它不會有'getResponseHeader'方法。 –

+2

歡迎來到SO,請不要將您的整個HTML粘貼到問題中。如果您只是添加相關部件,它可以讓所有人更容易 – Liam

回答

2

看起來像你使用錯誤的超載成功。

success : function(request) { 
    console.log(request); 
    var log = request.getResponseHeader("log"); 

應該是:

success : function(data, status, xhr) { 
    console.log(data); 
    var log = xhr.getResponseHeader("log"); 

docs

success Type: Function(PlainObject data, String textStatus, jqXHR jqXHR)

然後

The jqXHR Object

The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method.

1

成功函數返回(PlainObject data, String textStatus, jqXHR jqXHR)。你正在尋找的是最後一個參數,而不是響應本身。

success: function(request, status, xhr) { 
    console.log(xhr.getResponseHeader("log")); 
} 
+0

擊敗我! – Liam