我有一個angularJS形式,它將數據發佈到Java Servlet,但我沒有看到請求經過;沒有調用servlet「create」。AngularJS POST到服務器
這裏是我的代碼:
的test.html
<body>
<form ng-controller="UserController">
<legend>Create User</legend>
<label>Name</label>
<input type="text" id="name" name="name" ng-model="name" placeholder="User Name">
<label>Email</label>
<input type="text" id="email" name="email" ng-model="email" placeholder="ur email here">
<label>Password</label>
<input type="text" id="pwd" name="pwd" ng-model="pwd" placeholder="ur own pwd here">
<button ng-submit="createUser()" class="btn btn-primary">Register</button>
</form>
</body>
的script.js
function UserController($scope, $http) {
$scope.user = {};
$scope.createUser = function() {
$http({
method : 'POST',
url : '/create',
data : 'name=' + $scope.user.name + '&email=' + $scope.user.email,
headers : {
'Content-Type' : 'application/x-www-form-urlencoded'
}
})
}
我的servlet如下,但它不打印「張貼」一個。
public class FirstServlet extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
System.out.println("Get");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
System.out.println("Post");
}
}
Web服務器是碼頭,並在web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>createUser</servlet-name>
<servlet-class>servlet.FirstServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>createUser</servlet-name>
<url-pattern>/create</url-pattern>
</servlet-mapping>
</web-app>
我已經編輯我的答案,包括servlet代碼。這可能是我寫的最廣泛的答案,哈哈 – 2013-02-24 01:16:26
我相信他的問題是,ng-submit不在form標籤上,正如我在下面回答的。 – 2013-02-25 16:40:17
我把一個console.log內的createUser js函數。另外,你確定你的http調用可以解析爲適當的服務器url嗎?也許你可以在你的chrome開發工具網絡選項卡中檢查它?最後,在你的http文章中,你使用的是查詢字符串,不需要這樣做,只要嘗試@ChristianStewart建議的數據參數即可。 – mentat 2015-10-30 05:08:07