jQuery $ .ajax()请求返回空数据

时间:2013-12-12 17:19:39

标签: javascript ajax jquery

我有一个简单的登录页面,它将登录信息发送到servlet,并作为响应接收一个在jQuery中解释的参数。

正确发送数据,转到servlet,它也正确设置参数(我可以在响应头中的Firebug中看到)。

问题是当我想从返回的响应中检索数据并将其分配给JavaScript变量时。请求对象为空,而在Chrome检查中,我会看到警告:

  

未捕获的TypeError:对象没有方法'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>

2 个答案:

答案 0 :(得分: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

  

成功类型:Function(PlainObject数据,String textStatus,jqXHR   jqXHR)

然后

  

jqXHR对象

     

$ .ajax()返回的jQuery XMLHttpRequest(jqXHR)对象   jQuery 1.5是浏览器的原生XMLHttpRequest的超集   宾语。例如,它包含responseText和responseXML   属性,以及getResponseHeader()方法。

答案 1 :(得分:1)

成功函数返回( PlainObject data, String textStatus, jqXHR jqXHR )。你要找的是最后一个参数而不是响应本身。

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