0

I am using angularjs 1.6, and trying to create a url using this format

http://localhost:49524//api/products/GDN

where GDN is a parameter I wish to specify.

I have this working using $resource set up as:

$resource(appSettings.serverPath + "/api/products/:id", { id: '@id' });

And my controller contains:

productResource.query({id: vm.searchCriteria}, (data)=> {
        vm.products = data;
     });

I have tried renaming id to 'search'. However when I do so, the url generated becomes:

http://localhost:49524//api/products?search=GDN

Could someone suggest what I am I missing? Thanks!!

2 Answers 2

1

Why search is added as a query string ?

http://localhost:49524//api/products?search=GDN
                                       ^^^^^^^^^^

From docs angular#resource

Each key value in the parameter object is first bound to url template if present and then any excess keys are appended to the url search query after the ?.

In the productResource.query,search is there.But the this param is not defined in resource defination.

You have to rename it in angular#resource definition as well.

$resource(appSettings.serverPath + "/api/products/:search", { search: '@id' });
                                                    ^^^^^^^   ^^^^^^^                                                    

controller :

productResource.query({search: vm.searchCriteria}, (data)=> {
                       ^^^^^^
        vm.products = data;
     });

If yo dont rename it in resource defination,it will added as query string

EDIT :

$resource(appSettings.serverPath + "/api/products/:search", { search: '@id' });                                              
                                                   ^^^^^^     ^^^^^^
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for your comment Ritchie. However, here is a plunker that illustrates the issue

http://plnkr.co/edit/aQGMCHEa5EUvzhpDryio?p=preview

If I change the code as you suggested:

return $resource('/api/~:username/article/:id', {
 search: '@id',
 username: Authorization.currentUser
})

and

ctrl.testResource = function() {
Article.get({search: 5});
};

then the url becomes: http://run.plnkr.co/api/~dave/article?search=5

1 Comment

Thanks Ritchie. I think I also had a couple of other issues that were confusing the problem. I'm running using IISExpress, and sometimes I had to restart the server as the javascript changes were not reloaded. Also, within Chrome, I had to select 'Disable cache'. Thanks for your help!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.