$resource ...">

Nested parameters in angular request

I have an angular base resource (angular 1.0.7):

app.factory "User", [ "$resource", ($resource)-> $resource "/users/:id", { id: '@id' }, { index: { method: 'GET', isArray: false } } ] 

I can pass parameters like:

 User.index({ foo: 1, bar: 2 }) 

But I need to pass the nested parameters:

 User.index({ foo: { bar: 1 } }) 

And this fails because it sends:

 /users?foo=%5Bobject+Object%5D 

I tried:

 User.index({ foo: JSON.stringify({ bar: 1 }) }) 

But obviously, the parameters are not recognized on the server side (simple strings), and I would like to avoid parsing problems.

Do you have an elegant solution to this problem?


With jQuery, I would do:

 $.get("/users", { foo: { bar: 1 } } ) 

Production:

 /users?foo%5Bbar%5D=1 

Perfectly interpreted by the server.


It seems like a known issue ( here too). Now open the ugly patch ...

+6
source share
1 answer

Not as elegant as the changes coming to Angular, but you have to solve this by setting your parameter with the results of the function:

 User.index.get({ "foo": (function () { return JSON.stringify({ bar: 1 }); })() } ); 

The results in the HTTP request are similar to what you were looking for:

 ?foo=%7B%22bar%22:1%7D 

The whole Angular example:

 var app = angular.module('myApp', ['ngResource']); var restUrl = window.location + 'echo/json/'; app.factory('User', function($resource, $http){ return { index: $resource(restUrl) } }); function RestCtrl($scope, User) { $scope.url = restUrl; User.index.get({ "foo": (function () { return JSON.stringify({ bar: 1 }); })() } ); } 

http://jsfiddle.net/pherris/U8F8G/6/

+3
source

Source: https://habr.com/ru/post/953041/


All Articles