我使用了 AngularJS 和 Servlet 。单击“提交”按钮后,输入的数据将仅显示在URL栏中,但无法访问凭据Servlet。没有显示错误。我在下面提供了我的代码。谁能告诉我为什么无法访问servlet?
的index.html
<!DOCTYPE html>
<html>
<head>
<script src="resources/js/angular.min.js"></script>
<script>
var formApp = angular.module('formApp', []);
formApp.controller('formController', function($scope, $http) {
$scope.user = {};
$scope.submitForm = function() {
$http({
method : 'POST',
url : 'http://localhost:9090/UserAuthentication/Credentials',
data : JSON.stringify($scope.user),
headers : {
'Content-Type' : 'application/json'
}
}).success(function(data) {
alert("Controller is hit!");
});
};
});
</script>
</head>
<body>
<div ng-app="formApp" ng-controller="formController">
<h1>User Details</h1>
<form name="itemForm" ng-submit="submitForm()">
UserName: <input name="username" type="text" ng-model="user.username">
<br />Password: <input name="password" type="password"
ng-model="user.password"> <br /> <input type="submit"
value="Submit">
</form>
</div>
</body>
</html>
&#13;
凭据Servlet:
package com.controller;
import java.io.BufferedReader;
@WebServlet("/Credentials")
public class Credentials extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Credentials() {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String jsonData = " ";
BufferedReader reader = request.getReader();
String line = null;
while ((line = reader.readLine()) != null) {
jsonData += line + "\n";
}
System.out.println(jsonData);
}
}