0

I have been trying to call an API using $http.get method but its not calling that API instead just returning 404 error.

My $http

$http({
  method: 'POST',
  url: '/SalesManagement/SecuredSalesAPI/GetProductLine'
}).then(function(res) {
  debugger
  console.log(res.data);
}, function(header) {
  debugger
  console.log("HTTP Head: " + JSON.stringify(header));
});
//My route public class SalesManagementAreaRegistration : AreaRegistration { public override string AreaName { get { return "SalesManagement"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "SalesManagement_default",
"SalesManagement/{controller}/{action}/{id}", new { action = "Home", id = UrlParameter.Optional } ); } }

My API:

public class SecuredSalesAPI : Controller
{
    public JsonResult GetProductLine()
    {
        var sp = entity.spGetProductPerformance(startDate, end, "HPL");
        return Json(sp, JsonRequestBehavior.AllowGet);
    }
}

// and the namespace is 
// namespace WebApp.Areas.SalesManagement.Controllers
2
  • method: 'POST', | trying to call an API using $http.get <= Your code conflicts with your statement, which is it you are actually trying to do? Commented Apr 18, 2017 at 13:38
  • If you are not user what your route url is then test it directly from the browser first, this works great with http GET. If using a non GET end point test with Fiddler or PostMan. If you are still not sure what the end point actually is then install NuGet package Microsoft.AspNet.WebApi.HelpPage, this will create a help area in your site with a page that lists all your available web api end points including parameters, their types, and the urls. Commented Apr 18, 2017 at 14:30

3 Answers 3

1

You trying to get but you defined method as post.

$http({ method: 'GET', url: '/SalesManagement/SecuredSalesAPI/GetProductLine' }).then(function (res) { debugger console.log(res.data); }, function (header) { debugger console.log("HTTP Head: " + JSON.stringify(header)); });

Sign up to request clarification or add additional context in comments.

5 Comments

Check your route config inside area and check how route is defined
just updated my AreaRegistration. All other API and $http in other files are working but not this
can u try using $http.get('/SalesManagement/SecuredSalesAPI/GetProductLine').then(function(response){ }); Also the controller name should follow word controller. But its missing in your controller name. not in the url,, but in the classname
public class SecuredSalesAPIController : Controller. Add the keyword controller in the class name. Hope it will fix the issue. and call the api like before. $http({ method: 'GET', url: '/SalesManagement/SecuredSalesAPI/GetProductLine' }).then(function (res) {
Issac Johnson, SecuredSalesAPIController : Controller worked great. thanks for pointing me to the right direction.
0

Change the method to 'GET' instead of 'POST'

$http({
            method: 'GET',
            url: '/SalesManagement/SecuredSalesAPI/GetProductLine'
        }).then(function (res) {

            console.log(res.data);
        }, function (header) {

            console.log("HTTP Head: " + JSON.stringify(header));
        });

Also add keyword controller to your controller class name. ie; public class SecuredSalesAPIController : Controller { } and call the service from client side as before.

Comments

0

API only pass data by url.you have to use GET method to get data.

$http({
    method: 'GET',
    url: '/SalesManagement/SecuredSalesAPI/GetProductLine'
}).then(function(res) {

    debugger
    console.log(res.data);

}, function(header) {

    debugger
    console.log("HTTP Head: " + JSON.stringify(header));

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.