如何在JSON对象传递到angularjs资源

编程入门 行业动态 更新时间:2024-10-18 08:25:53
本文介绍了如何在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资源

本文发布于:2023-07-14 19:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   如何在   资源   JSON   angularjs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!