简体   繁体   English

AngularJS $ http与$ http.post

[英]AngularJS $http vs $http.post

I am new to AngularJS and trying my hands on it. 我是AngularJS的新手,正在尝试这一工作。

I want to send data to server and map it to DTO in one of the REST Service. 我想将数据发送到服务器并将其映射到REST服务之一中的DTO。 So I did it in this way, 所以我是这样做的

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

Payload going as 有效负载为

{"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 Service 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();
}

Everything is working fine. 一切正常。

Now I tried with this way as generalised function for all the server calls and it stopped working 现在,我尝试使用这种方法作为所有服务器调用的通用函数,但该方法停止了工作

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

And it stopped working. 它停止工作了。 payload going as: 有效负载为:

[object Object],[object Object]

I tried JSON.stringify and then it passed as 我尝试了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"
    }
]

I dont know the correct working of $http vs $http.post, is there any specific things that need to take care while each way of sending data. 我不知道$ http vs $ http.post的正确工作,在每种发送数据的方式中是否有任何需要特别注意的事情。

$http.post is just a shortcut method for $http with method:'POST' . $http.post只是$http的快捷方式,方法为method:'POST' In your example, running 在您的示例中,运行

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

should yield exactly the same result as your original $http.post request 应产生与原始$http.post请求完全相同的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM