Wednesday, July 12, 2017

Calling Web API not working in AngularJS using $http service

When calling ASP.NET Web API service inside the solution, I encountered an issue that is 404 not found. Given that this issue persists, I tried adding a forward slash before the url in the Ajax call which works.
AngularJS
$http({
        //url: "EmployeeRoute/GetAll", //404 error
 url: "/EmployeeRoute/GetAll",
 dataType: 'json',
 method: 'POST',
 data: GetAll,
 headers: {
  "Content-Type": "application/json"
 }
}).then(function (resp) {
 if (typeof resp.data === 'object') {
  return resp.data;
 } else {
  return $q.reject(response.data);
 }
}, function (err) {
 return $q.reject(err.data);
});
I also make sure that the WebApiConfig.Register method gets executed in Global.asax.cs.
Global.asax.cs
protected void Application_Start(object sender, EventArgs e)
{
 GlobalConfiguration.Configure(WebApiConfig.Register);
}

0 comments:

Post a Comment