如何将 JSON 对象传递给 angularjs 中的资源 [英] How to pass in a JSON object to a resource in angularjs

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

问题描述

我想使用一个 Web 服务,它在 POST 请求中接收 JSON 对象(该对象绑定两个数组)并返回一个 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 向网络服务发送请求.这是我的代码:

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,这意味着我传递的参数将作为 null 传递.

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

如何传入此对象,以便服务器将它们解释为包含两个数组的对象.我是 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 中包含参数,因为 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"]})

注意 {} 作为第一个参数,它是您请求的参数.第二个 {} 用于负载.这将在请求有效负载中发送您的 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天全站免登陆