如何在JSON对象传递到angularjs资源 [英] How to pass in a JSON object to a resource in angularjs

查看:103
本文介绍了如何在JSON对象传递到angularjs资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个Web服务,它在JSON对象(Object绑定两个阵列)的POST请求,并返回一个JSON对象数组。

I would like to use a web-service that takes in JSON object (The object binds two arrays) in a POST request and returns a JSON object array.

我现在想从我的AngularJS发送请​​求到web服务。这里是我的code:

I would now like to send request to the webservice from my AngularJS. Here is my code:

wellApp.factory('Search', ['$resource',function($resource){

    return $resource('/filetables/searchdata/:tagSearchInput',
            {
            },
            {
                searchData:{
                    method:'POST',
                    isArray: true,
                    params:{ tag: '@tagSearchInput.tag',
                            details: '@tagSearchInput.details'
                    }
                }
            })

}])


function myWellsCtrl($scope,$resource, Wells, Tags, Search) {

    $scope.wellSearchResult = Search.searchData({tag: ["TypeOfWell"],
                                                 details: ["Vertical"]});
};

如果我这样做,我在服务器端得到一个NullPointerException异常,这意味着我传递参数越来越为空过去了。

If I do this, I get a NullPointerException at the server side, meaning that the arguments that I pass are getting passed as null.

我如何传递这个对象使得服务器间$ P $其中pts它们含有两个数组的对象。我是新来AngularJS,我不能够理解分配传入的参数的@符号。这将会是巨大的,如果有人在这里可以借给我一些帮助。

How do I pass in this object such that the server interprets them as an object containing two arrays. I'm new to AngularJS and am not able to understand the @ notation of assigning the incoming parameter. It'd be great if someone here can lend me some help.

推荐答案

您不应该需要在searchData PARAMS因为JSON将在POST请求的主体发送。尝试从searchData删除它们。然后在你的控制器,尝试调用你的服务是这样的:

You shouldn't need to include params in searchData since the JSON will be sent in the body of the POST request. Try removing them from searchData. Then in your controller, try calling your service like this:

Search.searchData({}, {tag: ["TypeOfWell"], details: ["Vertical"]})

注意{}作为第一个参数,这是你的要求的PARAMS。第二个{}是有效载荷。这将在请求负载而不是作为PARAMS发送您的JSON。让我知道,如果不为你工作。我做了类似于你在做什么东西,这种方式为我工作。

Note the {} as the first parameter, which is the params for your request. The second {} is for the payload. This will send your JSON in the request payload instead of as params. Let me know if that doesn't work for you. I have done something similar to what you're doing and this way worked for me.

这篇关于如何在JSON对象传递到angularjs资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆