0

I am calling a web API with angularJS Service.

My webp API method is working fine without parameters '/api/Subpart/'

When i tried to pass parameters , Web API method is not firing.

Angular service

.service('subpartService', ['$http', function ($http) {
     this.getSubparts = function (val) {
       return $http.get('/api/Subpart/'+val); // With parameters not working
     };
 }])

Web API Controller Action

[Route("api/Subpart/{strsubpart}")]
public List<tbl_Employee> GetSubparts(string strsubpart)
{
    Entities db = new Entities();
    var data = db.tbl_Regulation.Where(c => c.Subsection == "ABC");
    return data.ToList();
}
Shakeer Hussain
  • 2,230
  • 7
  • 29
  • 52
  • 1
    What else have you tried. Can you call it with a parameter from your browser? Are you sure that `val` has a valid non empty/ non null / non undefined string value? Also what does `without parameters '/api/Subpart/'` mean? This is a route and not a parameter, you cant call the method without it so what do you mean it was working fine without it? – Igor Oct 06 '16 at 18:11
  • Ohh Very Strange. Its working, When i pass my api Url as 'api/Subpart/abc' – Shakeer Hussain Oct 06 '16 at 18:14
  • Is the angular service running on the same web site as where the web api is hosted? If not you will need to provide the complete host address as well. – Igor Oct 06 '16 at 18:15
  • actually my parameter value coming as 'api/Subpart/1301.2(b)' because of 1301.2(b) its not working – Shakeer Hussain Oct 06 '16 at 18:16
  • How to handle this format 1301.2(b) in api url? – Shakeer Hussain Oct 06 '16 at 18:17
  • try this edit: return $http.get('/api/Subpart?strsubpart='+val); – Feras Salim Oct 06 '16 at 18:17
  • Check your error logs. Also inspect the ajax request in browser dev tools network for clues – charlietfl Oct 06 '16 at 20:02
  • If it was me I would look for a different identifier to send...or use POST – charlietfl Oct 06 '16 at 20:04

2 Answers2

0

I have the solution.It has pass parameters with ?

this.getSubparts = function (val) {
           var subUrl = "http://localhost/api/Subpart/sub_part?subpart="+ val;
             return $http.get(subUrl);
         };
Shakeer Hussain
  • 2,230
  • 7
  • 29
  • 52
0

Try to "escape" the value.

http://www.w3schools.com/jsref/jsref_encodeuri.asp

this.getSubparts = function (val) {
           var subUrl = "http://localhost/api/Subpart/?subpart="+ encodeURI(val);
             return $http.get(subUrl);
         };
granadaCoder
  • 26,328
  • 10
  • 113
  • 146