how to access form data in servlet using angular js

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-06 19:30:50

问题


I am new to angular js. please help me with the below issue I have a form with a controller loginController. I have two text boxes user.email and user.password. I want to post the data to servlet onclick of the Log in button. I have written the following code, but when i click Log in, the data is not getting posted on to the server. Also there is no error message.

Below is my JSP file `

<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 File

`

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;
        });
    }

});

` I have tried all the solutions that are available but couldn't find any result.. Kindly help on this


回答1:


Your <button> does not cause the form to submit. Change to <input type="submit" ng-disabled="login.$invalid">Log in</button> or remove ng-submit from the form and add ng-click="login()" to the <button>.



来源:https://stackoverflow.com/questions/31108048/how-to-access-form-data-in-servlet-using-angular-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!