$ resource POST错误400

时间:2017-02-03 16:38:32

标签: angularjs resources

大家好,我正在使用angular $ resource进行POST调用。这是我的FE功能

$scope.showPrompt = function (ev) {
        $scope.usernameSelected = [];
        $scope.users.forEach(function (element) {
            if (element.checked)
                $scope.usernameSelected.push(element);
            console.log($scope.usernameSelected);
        });
        var conferma = $mdDialog.prompt()
                .title('Insert group name?')
                .clickOutsideToClose(true)

            //.textContent('Bowser is a common name.')
            .placeholder('Group name')
            .ariaLabel('Group name')
            .targetEvent(ev)
            .ok('Crea gruppo')
            .cancel('Annulla');

    $mdDialog.show(conferma).then(function (result) {
            GroupService.group({}, {
            creatore: $cookieStore.get('username'),
            gruppo: result,
            partecipanti: $scope.usernameSelected},
        function (data) {
            console.log("GRUPPO", data);
        });
        $scope.status = 'NOMEGRUPPO: ' + result + '.';        //inserito il nome gruppo
    }, function () {

        $scope.status = 'INSERT NOME GRUPPO';
    });
};

这是GroupService:

var groupService = angular.module("groupService",['ngResource']);

groupService.factory("GroupService",['$resource',
   function($resource){
         var url = "";
        return $resource("",{},{
                group: {method : "POST", url:url+"group", isArray: false},
                findGroup: {method : "GET", url:url+"findGroup", isArray: true}
        });    
}]);

我有这个错误:

angular.js:14328 Possibly unhandled rejection: {"data":"<!DOCTYPE html><html><head><title>Apache Tomcat/8.5.11 - Error report</title><style type=\"text/css\">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style> </head><body><h1>HTTP Status 400 - </h1><div class=\"line\"></div><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p><hr class=\"line\"><h3>Apache Tomcat/8.5.11</h3></body></html>","status":400,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"group","data":{"creatore":"ciro","gruppo":"nuovo","partecipanti":[{"id":0,"username":"a","email":"a@aa","password":null,"name":"a","surname":"a","longitude":0,"latitude":0,"checked":true},{"id":0,"username":"ciao","email":"ciao@ciao","password":null,"name":"ciao","surname":"ciao","longitude":0,"latitude":0,"checked":true}]},"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":""}

这是我的BE代码。我有一个管理客户端请求的spring控制器

@RequestMapping(value = "/group", method = RequestMethod.POST)
    public @ResponseBody
    Group createGroup(@RequestBody Group json, HttpServletRequest request) throws SQLException, ClassNotFoundException {

    Group g = DBUtils.insertGroup(json.getPartecipanti(), json.getGruppo(), json.getCreatore());
    return g;
}

这是我的DBUtils.insertGroup

public static Group insertGroup(List<String> members, String groupName, String userCreator) throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn;

        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        String sql = "insert into gruppo \n"
                + " (idCreatorUser,groupName,idPartecipante,data) values (?,?,?,?) ";

        PreparedStatement pstm = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        java.util.Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());

        Utente creator = DBUtils.findUser(userCreator);

        pstm.setInt(1, creator.getId());
        pstm.setString(2, groupName);
        pstm.setInt(3, creator.getId());
        pstm.setTimestamp(4, timestamp);
        int res = pstm.executeUpdate();
        ResultSet rsId = pstm.getGeneratedKeys();
        rsId.next();
        int idGruppo = rsId.getInt(1);

        Group response = new Group();
        response.setIdGruppo(idGruppo);
        response.setGruppo(groupName);
        response.setIdCreatore(creator.getId());
        response.setData(timestamp);
        response.setPartecipanti(members);
        response.setCreatore(creator.getUsername());
        System.out.println(response);

        sql = "insert ignore into gruppo \n"
                + " (idGruppo,idCreatorUser,groupName,idPartecipante,data) values (?,?,?,?,?) ";
        pstm = conn.prepareStatement(sql);
        Utente part;
        for (int i = 0; i < members.size(); i++) {
            pstm.setInt(1, idGruppo);
            pstm.setInt(2, creator.getId());
            pstm.setString(3, groupName);
            part = DBUtils.findUser(members.get(i));
            pstm.setInt(4, part.getId());
            pstm.setTimestamp(5, timestamp);
            pstm.executeUpdate();
        }

        conn.close();
        return response;

    }

我想发送一些参数,但我无法弄清楚语法错误是什么。

0 个答案:

没有答案