2017-04-18 70 views
0

我一直在尝试使用$ http.get方法调用API,但不调用该API,而是仅返回404错误。

我的$ 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 }); } }

我的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 
+0

'方法:'POST','| '试图使用$ http.get'调用一个API <=您的代码与您的语句冲突,这是您实际尝试执行的操作吗? – Igor

+0

如果你不是用户的路由网址,那么首先直接从浏览器进行测试,这对于http GET很有效。如果对Fiddler或PostMan使用非GET终点测试。如果你仍然不确定实际的端点是什么,然后安装[NuGet包Microsoft.AspNet.WebApi.HelpPage](https://www.nuget.org/packages/Microsoft.AspNet.WebApi.HelpPage/),这将在网站上创建一个帮助区域,其中列出了所有可用的Web API端点,包括参数,类型和网址。 – Igor

回答

0

更改方法 'GET',而不是 'POST'

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

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

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

同时添加关键字控制器,控制器类名。即; public class SecuredSalesAPIController:Controller { } 并像以前一样从客户端调用服务。

1

你想了解我,但你定义的方法后。

$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)); });

+0

我尝试过GET,但没有运气。 – Saleem

+0

检查你的区域内的路由配置,并检查如何定义路由 –

+0

刚刚更新了我的AreaRegistration。其他文件中的所有其他API和$ http正在工作,但不是这个 – Saleem

0

API只能通过url传递数据。您必须使用GET方法来获取数据。

$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)); 

});