2017-04-18 12 views
0

$ http.getメソッドを使用してAPIを呼び出そうとしていますが、代わりに404エラーを返す代わりにAPIを呼び出すようにしています。

マイ$ 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

あなたのルートurlがユーザからブラウザで直接テストされているのであれば、これはhttp GETでうまくいきます。 FiddlerまたはPostManで非GETエンドポイントテストを使用している場合。エンドポイントが実際に何であるかわからない場合は、[NuGet package Microsoft.AspNet.WebApi.HelpPage](https://www.nuget.org/packages/Microsoft.AspNet.WebApi.HelpPage/)をインストールしてください。パラメータ、タイプ、URLなど、使用可能なすべてのWeb APIのエンドポイントを一覧表示するページで、サイト内にヘルプエリアを作成します。 – Igor

答えて

0

の変更方法は、お使いのコントローラのクラス名に 'POST'

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

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

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

また追加キーワードコントローラの代わりに 'GET' に。すなわち、 public class SecuredSalesAPIController:コントローラ { } と同様にクライアント側からサービスを呼び出します。

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

}); 
関連する問題