[英]jQuery $.ajax() request returns empty data
I have a simple login page that sends the login information to the servlet, and in response receives one parameter which is interpreted in jQuery. 我有一个简单的登录页面,该页面将登录信息发送到servlet,并作为响应接收一个在jQuery中解释的参数。
Data is sent correctly, goes to the servlet which also sets the parameter correctly (I can see that in the Firebug in the response header). 数据已正确发送,并转到了servlet,该servlet也正确设置了参数(我可以在响应头的Firebug中看到该信息)。
The problem is when I want to retrieve data from the returned response and assign it to a JavaScript variable. 问题是当我想从返回的响应中检索数据并将其分配给JavaScript变量时。 The request object is empty, while in Chrome inspect I see an alert:
请求对象为空,在Chrome检查中,我看到一条警报:
Uncaught TypeError: Object has no method 'getResponseHeader'.
未捕获的TypeError:对象没有方法'getResponseHeader'。
When I display it using console.log ()
does not return anything to me, no value. 当我使用
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>
Looks like your using the wrong overload for success. 看起来您使用了错误的重载来获得成功。
success : function(request) {
console.log(request);
var log = request.getResponseHeader("log");
should be: 应该:
success : function(data, status, xhr) {
console.log(data);
var log = xhr.getResponseHeader("log");
success Type: Function( PlainObject data, String textStatus, jqXHR jqXHR )
成功类型:函数(PlainObject数据,字符串textStatus,jqXHR jqXHR)
Then 然后
The jqXHR Object
jqXHR对象
The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object.
从jQuery 1.5开始,由$ .ajax()返回的jQuery XMLHttpRequest(jqXHR)对象是浏览器本机XMLHttpRequest对象的超集。 For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method.
例如,它包含responseText和responseXML属性,以及getResponseHeader()方法。
The success function returns ( PlainObject data, String textStatus, jqXHR jqXHR )
. 成功函数返回
( PlainObject data, String textStatus, jqXHR jqXHR )
。 What you are looking for is the last parameter and not the response itself. 您要查找的是最后一个参数,而不是响应本身。
success: function(request, status, xhr) {
console.log(xhr.getResponseHeader("log"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.