从AngularJS向Restful Web服务发布请求

时间:2016-04-09 01:51:26

标签: angularjs

我在AngularJS中创建登录页面,提交表单后,数据将发布到Restfull Web服务以检查帐户是否存在,如果成功返回"成功"并返回"失败"如果失败

网络服务:

@POST
@Path(value = "login")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML)
public String login(@FormParam("email")String email, @FormParam("pass") String pass){
    UserModel um = new UserModel();
    Nguoichoi nc = um.find(email.trim(), pass.trim());
    if (nc != null){
        return "Success";
    } else { 
        return "Failed";
    }
}

AngularJS功能:

app.controller("LoginController", function($scope,$http) {
        $scope.login = function(){
            var encodeString = "email=" + this.email + "&pass=" + this.pass;
            $http({
                method: "POST",
                url: "http://localhost:8080/HocvuiWebservices/rest/user/login",
                data: encodeString,
                headers: {'Accept':' text/plain', 'Content-Type': "application/x-www-form-urlencoded"}
            }).success(function(data, status, headers, config){
                console.log(data);
            }).error(function(data, status, headers, config){
                console.log(data);
                console.log(status);
                console.log(headers);
                console.log(config);
                console.log("Error submit form");
            });
        }
    });

表格登录:

<form class="login-form" ng-submit="login()">
                <div class="form-group">
                    <label for="exampleInputEmail1">Email: </label> <input
                        type="email" class="form-control" ng-model="email"
                        placeholder="Email">
                </div>
                <div class="form-group">
                    <label for="exampleInputPassword1">Password: </label> <input
                        type="password" class="form-control" ng-model="pass"
                        placeholder="Password">
                </div>
                <div class="form-group">
                    <div class="checkbox">
                        <label> <input type="checkbox"> Remember me
                        </label>
                    </div>
                    <button type="submit" class="btn btn-primary">Loginp</button>
                    &nbsp;&nbsp;&nbsp;<a ui-sref="dangky" class="text-primary">Register</a>
            </form>

但是当我提交表单时,发布请求返回错误&#34;错误提交表单&#34;,尽管发布请求将成功连接到Restful Web服务?

0 个答案:

没有答案