Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

query string in $resource url

Tags:

angularjs

my service has to use a query string due to limitations on the server that runs classic ASP:

angular   .module('myServices', ['ng', 'ngResource'])   .factory('Item', ['$resource',      function ($resource) {          return $resource('/api/?p=item/:id');      }]); 

and I want to add extra query string parameters to it:

Item.query({test: 123}, on_success, on_error); 

but the resulting url is

/api/?p=item?test=123 

apparently there is a bug, but how to get around it?

EDIT: filed this at https://github.com/angular/angular.js/issues/1511

like image 456
akonsu Avatar asked Oct 30 '12 20:10

akonsu


People also ask

What is a query string in a URL?

A query string is the portion of a URL where data is passed to a web application and/or back-end database. The reason we need query strings is that the HTTP protocol is stateless by design. For a website to be anything more than a brochure, you need to maintain state (store data).

How do you query parameters in a URL?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.

How do you separate a query string from a URL?

The query string is composed of a series of field-value pairs. Within each pair, the field name and value are separated by an equals sign, " = ". The series of pairs is separated by the ampersand, " & " (or semicolon, " ; " for URLs embedded in HTML and not generated by a <form>...

Does URI include query string?

There is no protocol information given in URI. It contains components such as protocol, domain, path, hash, query string, etc.


1 Answers

You can use resource parameters. If you haven't specified placeholders in the path, they would automatically be converted into query string params. Like that:

angular     .module('myServices', ['ng', 'ngResource'])     .factory('Item', [          '$resource',          function ($resource) {              return $resource('/api');      }]);  Item.query({p: 'item/1'}); 

This would result in a request to /api?p=item/1.

P.S.

I suppose you already know that, but you don't like it. But I still think this is the correct way in your case. Considering the bad API design you are dealing with that back-end you could wrap the AngularJS resources with another service which does this for you.

like image 97
Haralan Dobrev Avatar answered Sep 18 '22 15:09

Haralan Dobrev