2015-06-29 73 views
0

我是angular js的新手。請幫我解決下面的問題 我有一個控制器loginController的表格。我有兩個文本框user.emailuser.password。 我想將數據發佈到Log in按鈕的servlet onclick。 我寫了下面的代碼,但是當我點擊Log in時,數據沒有被髮布到服務器上。也沒有錯誤信息。如何使用angular js訪問servlet中的表單數據

下面是我的JSP文件 `

<html ng-app="practiceApp"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
<link rel ="stylesheet" type="text/css" href="bootstrap.min.css"/> 
</head> 
<body> 
    <script type="text/javascript" src="angular.min.js"></script> 
<script type="text/javascript" src="practice.js"></script> 
    <form name='login' ng-controller='loginController' ng-submit="login()"> 
     <div> 
     {{hello}} 
      <label>Email</label> 
      <input type="email" name="email" ng-model="user.email" required/> 
      <span ng-show='login.email.$dirty && login.email.$error.required'>Required</span> 
      <span ng-show='login.email.$dirty && login.email.$error.email'>Not a valid email</span> 
      <label>Password</label> 
      <input type="password" name="password" ng-model="user.password" required/><br/> 
      <span ng-show='login.password.$dirty && login.password.$error.required'>Required</span> 
      <button ng-disabled="login.$invalid">Log in</button> 
     </div> 
    </form> 
</body> 
</html> 

`

JavaScript文件

`

var app = angular.module('practiceApp',[]); 
app.controller('loginController',function($scope,$http){ 
    $scope.login = function(){ 

     $http({ 
      method: 'POST', 
      url:'/login', 
      headers:{'Content-Type':'application/json'}, 
      data:$scope.user 
     }).success(function(data){ 
      $scope.status=data; 
     }); 
    } 

}); 

` 我已經嘗試了所有可用,但couldn解決方案找不到任何結果..請幫助o n

+0

您是否在網絡標籤中看到POST請求? –

回答

0

您的<button>不會導致表單提交。更改爲<input type="submit" ng-disabled="login.$invalid">Log in</button>或從表格中刪除ng-submit,並將ng-click="login()"添加到<button>

+0

我試過了,但沒有運氣。當我在網頁瀏覽器中嘗試時,我得到了下面的錯誤'無法讀取null的屬性0' – axn7975

+0

@ axn7975您的窗體'名稱'也與登錄函數名衝突。將表單名稱更改爲「loginForm」或與登錄函數名稱不同的內容。 – Strelok