1 回答
TA贡献1839条经验 获得超15个赞
好的,所以我不熟悉 angular,但只是查看ajax的w3,您需要处理这样的响应:
将 id 添加到您的 span 元素(${connect.error}从中删除):
<span id="somediv"></span>
添加然后成功:
<script type="text/javascript">
var app = angular.module("AHS", []);
app.controller("myCtrl", function ($scope, $http, $httpParamSerializerJQLike){
$scope.userObj = {
mail: "",
password: "",
}
$scope.submitForm = function() {
$http({
method : 'POST',
url : '/', // use relative
data: $httpParamSerializerJQLike($scope.userObj),
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;'
}
}).then(function mySuccess(response) {
//handle response here
console.log(response.data);
document.getElementById("somediv").innerHTML = response.data;
});
};
});
</script>
然后对于您的 servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
signInForm connect = new signInForm();
User user = connect.validateSignIn(request);
HttpSession session = request.getSession();
if (user != null) {
// request.setAttribute("connect", connect); //what the hell is connect supposed to be here?
session.setAttribute("sessionUser", user);
// this.getServletContext().getRequestDispatcher("/restrict_client/client.jsp").forward(request, response); //you do not do this in ajax requests.
}else{
//request.setAttribute("connect", connect);
//this.getServletContext().getRequestDispatcher("/signIn.jsp").forward(request, response);
}
response.setContentType("text/plain"); // Set content type
response.setCharacterEncoding("UTF-8");
response.getWriter().write("hello world"); // Write response body.
}
添加回答
举报