2

I'm trying to call a web api POST method using angular js it get call. I try from postman utility it also get call and in respose give a json data.But in angular side i am not getting data in response.

my services in angular js

 var _getProjects = function (searchText) {
    debugger;
    return $http.post(URL + 'api/project/GetProjectDetails?searchText='+ searchText).then(function (response) {
        return response;
    });
};

and below is my web api

 [HttpPost]
    [Route("api/project/GetProjectDetails")]
    public List<GetKeywordsBasisData_Result> GetProjectDetails(string searchText)
    {
        if (!String.IsNullOrEmpty(searchText))
        {
            return projectProvider.GetProjectDetails(searchText).ToList();
        }
        return new List<GetKeywordsBasisData_Result>();
    }
Mayur Singh
  • 121
  • 10
  • Open inspect element and click on network tab. Now click on the button. That will tell you which url are you calling. Copy that url and compare with your postman url. – Alaksandar Jesus Gene Apr 07 '16 at 13:43
  • To open inspect element right click on the browser – Alaksandar Jesus Gene Apr 07 '16 at 13:44
  • Why do you call return twice in your _getProjects $http.post? – Frederick Motte Apr 07 '16 at 13:48
  • 1
    Multiple comments here: 1) the post data structure, all I see is a string. Can we also see the signature of the WebApi? 2) routing might not be correct, can you include routing info for your web api call in your .net code? 3) You never actually do anything with the response, you just return it but that does not do anything. You need a callback method that you can pass the results to. Finally, for your question the only real code of interest is the WebApi controller structure and the angular factory code. – Igor Apr 07 '16 at 13:48
  • See previous so answer: [Post parameter is always null](http://stackoverflow.com/a/13030451/1260204). This does not address the other possible problems I mentioned. – Igor Apr 07 '16 at 13:58
  • ya but now the web api get call but in postman it give a response .but in angular js respose it says undefined – Mayur Singh Apr 07 '16 at 14:05
  • You need a callback. The response from your post happens async at a later point in time. You want to pass your function a function to call after you get a response. That function should accept whatever it is you are giving back so you can do something with it. – Igor Apr 07 '16 at 14:08
  • 1
    basically my web request not get access the web api because of CORS files are missing but after adding in web api issue get resolved, Thanks to all for helping to resolve the issues – Mayur Singh Apr 07 '16 at 16:17

1 Answers1

0

You are missing a callback. Http Calls on $http happen asynchronously, you need to pass in a method to call once the call completes. Something like this. The parameter myCallback is a function that takes in the expected response, you can then process it (or whatever) inside that function.

 function (searchText, myCallback) {
    $http.post(URL + 'api/project/GetProjectDetails?searchText='+ searchText).then(function (response) {
        myCallback(response);
    });
};

You can then call it from your controller like this assuming getProjects is your factory method. Also assuming that $scope is a field on your controller.

var me = this;
getProjects('somethingToSearchFor', function(results){
    me.$scope.results = results;
});
Igor
  • 60,821
  • 10
  • 100
  • 175
  • 1
    `.success` is depricated. https://docs.angularjs.org/api/ng/service/$http#deprecation-notice. I'd argue that with this code that you'd much rather just want to return the promise. `return $http.post(URL);` but it does render the entire function pretty useless. – ippi Apr 07 '16 at 14:19
  • in console it show like this project:1 XMLHttpRequest cannot load http://localhost:53018/api/project/GetProjectDetails?searchText=ios. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:49938' is therefore not allowed access. – Mayur Singh Apr 07 '16 at 14:27
  • @MayurSingh are you using chrome ? see [this](http://stackoverflow.com/questions/28547288/no-access-control-allow-origin-header-is-present-on-the-requested-resource-err) – Lulylulu Apr 07 '16 at 14:39
  • Sir the api is not call because of CORS plugin are missing . because my request get block when i call the api.After adding plugin files its work and results is shown – Mayur Singh Apr 07 '16 at 16:15
  • @MayurSingh - glad to hear it! – Igor Apr 07 '16 at 17:26
  • @Igor please vote my question so that i can get some reputation points – Mayur Singh Apr 08 '16 at 06:08
  • @ippi please vote my question so that i can get some reputation points – Mayur Singh Apr 08 '16 at 06:11