Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am building a small app using AngularJS and WebAPI.

Usecase: I am passing a string parameter from the drop-down ng-change event to a function called getcatdetails(selectedcategory) in controller.js and service.js calls WEBAPI.

Problem: When I add additional method in WEB API the code doesn't get fired. When I put the break point in controller and Service.js it looks fine but the call doesn't happen from Service to WEBAPI.

Here is the code..

HTML:

<select ng-model="selectedcategory" ng-options="item for item in catlist" ng-change="getcatdetails(selectedcategory)">
    <option value="">Select</option>
</select>

Controller.js

 $scope.getcatdetails = function (selectedcategory) {
    var catdetails = CategoryService.getcatdetails(selectedcategory);
 }

Service.js

this.getcatdetails= function (selectedcategory) {
    return $http.get("/api/ProductAPI/getcatdetails" + selectedcategory);
}

WEB API method

[Route("/api/ProductAPI/getcatdetails")]
public string getcatdetails(string selectedcategory)
{
    return selectedcategory;
}

Please help me ...i am totally struck


This is what I tried by I am still unable to call WEB API method..

Service.js

 this.getcatdetails = function () {
   return $http({ method: 'GET', url: 'api/productapi/getcatdetails', data: '"' + selectedcategory + '"' })
}


WEB API Method..

    [Route("api/ProductAPI/getcatdetails/{selectedcategory}")]
   public string getcatdetails(string  selectedcategory){

       return selectedcategory;

}

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You can pass the category as part of the url, so chnage your method to be [Route("api/ProductAPI/getcatdetails/{selectedcategory} ")] public string getcatdetails(string selectedcategory) {

    return selectedcategory;
}
share|improve this answer

Try:

$http({method:'GET', url:'api/productapi/getcatdetails', data:'"' + selectedcategory + '"'})

And on your server:

 public string getcatdetails([FromUri] string  selectedcategory){
  ...
 }
share|improve this answer
1  
You cannot send any data in your message body with GET method. –  Omar.Alani Dec 25 '14 at 4:22
    
You're not trying hard enough –  pixelbits Dec 25 '14 at 4:37
    
With GET query, you can only send data through query string, so your method of the web api should use [FromUri] not [FromBody]. Go and try it. –  Omar.Alani Dec 25 '14 at 5:34
    
Corrected, thanks –  pixelbits Dec 25 '14 at 5:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.