AngularJS $ http vs $ http.post

时间:2014-11-05 12:46:21

标签: java jquery angularjs angularjs-directive

我是AngularJS的新手,并尝试着它。

我想将数据发送到服务器并将其映射到其中一个REST服务中的DTO。 所以我这样做了,

         saveUserUrl = "http://localhost:8080/App/rest/UserManager/saveUser";

         var results = [];
         for (var i = $scope.users.length; i--;) {
             var user = $scope.users[i];
             results.push(user);
         }
         $http.post(saveUserUrl, {
                data: results,
                headers: {'Content-Type': undefined,'Authorization':$routeParams.auth },
                transformRequest: function(data, headersGetterFunction) {
                    return data;
                }
            }).
            success(function(data, status, headers, config) {
                if(status == 203) {
                    alert("Session Timed out, Please Login Again");
                    $window.location=getContextFromURL();
                }else{
                    alert("user saved success");
                }
            }).
            error(function(data, status, headers, config) {
                alert("user saved failure");
            });

有效负载为

{"data":
    [{"id":2,"name":"Agent3","extn":3,"group":1,"groupName":"Agent","isNew":false},
     {"id":1,"name":"Agent2","extn":"2","group":1,"groupName":"Agent","isNew":false}
    ],
 "headers":
    {"Authorization":"5825ccb2-d28b-47e6-87a8-0b31f8ec9a78"}
}

REST服务

@POST
@Path("/saveUser")
@Produces({ MediaType.APPLICATION_JSON} )
@Consumes({ MediaType.APPLICATION_JSON} )
public Response saveUser(@HeaderParam(HttpHeaders.AUTHORIZATION) String authorization,@Context HttpServletRequest req, DBUserEntity dbUser  ) {
    System.out.println("User iiss :" + dbUser);
    ResponseBuilder responseBuilder = Response.status(200);
    //System.out.println("zipStatus :"+zipStatus);
    //TODO
    return responseBuilder.entity(null).build();
}

一切都很好。

现在我尝试使用这种方式作为所有服务器调用的通用函数,并且它停止工作

$http({ 
            url: urlBase+requestMapping, 
            method: method, 
            data: formData, 
            headers: { 
                'Content-Type': contentType, 
                'Authorization':$routeParams.auth
            }, 
            transformRequest: function(data, headersGetterFunction) {
                return data; // do nothing! FormData is very good!
            }
        });

它停止了工作。 有效载荷如下:

[object Object],[object Object]

我尝试了JSON.stringify,然后将其作为

传递
[
    {
        "id": 2,
        "name": "Agent23",
        "extn": 23,
        "group": 1,
        "groupName": "Agent",
        "isNew": false,
        "$$hashKey": "object:11"
    },
    {
        "id": 1,
        "name": "Agent22",
        "extn": "22",
        "group": 1,
        "groupName": "Agent",
        "isNew": false,
        "$$hashKey": "object:10"
    }
]

我不知道$ http vs $ http.post的正确工作,在每种发送数据的方式中是否需要注意任何具体的事情。

1 个答案:

答案 0 :(得分:0)

$http.post只是$http method:'POST'的快捷方式。 在您的示例中,运行

$http({ 
        url: saveUserUrl, 
        method: 'POST', 
        data: results,
        headers: {'Content-Type': undefined,'Authorization':$routeParams.auth },
        transformRequest: function(data, headersGetterFunction) {
            return data;
        }
    });

应该会产生与原始$http.post请求

完全相同的结果