2017-05-22 12 views
-1

私はangularjsとwebapiの初心者です。私はangularjsとwebapiの下に作成しました。私が直面している問題は、apiオブジェクトにnullとデータが含まれていないことです。誰でも私が間違っていることに気づくことができます。

角度コード:

$scope.GetReport = function() { 

    var ReportModel = {}; 
    debugger; 
    var result = {}; 

    if (angular.isDefined($scope.Report.FromDate)) 
     ReportModel.FromDate = new Date($scope.Report.FromDate); 
    else 
     ReportModel.FromDate = null; 
    if (angular.isDefined($scope.Report.ToDate)) 
     ReportModel.Todate = new Date($scope.Report.ToDate); 
    else 
     ReportModel.Todate = null; 
    ReportModel.UserID = ''; 
    ReportModel.UserWWID = '1234'; 
    ReportModel.UserRole = ''; 
    ReportModel.ProjectType = ''; 
    ReportModel.ProjStatus = 'In Progress'; 
    ReportModel.CreatedBy = '11744439'; 
    ReportModel.LGroup = ''; 
    ReportModel.LUnit = ''; 
    ReportModel.LTeam = ''; 

    var config = { 
     headers: { 'Content-Type': 'application/json;charset=utf-8' }, 
     datatype: JSON, 
     data: JSON.stringify(ReportModel) 
    }; 

    result = $http.get('api/Project/GetReport', { params: ReportModel }) 
       .then(function (response) { 
        result = response.data; 
        }) 
       }, function (response) { 
        alert('Failed ' + JSON.stringify(response.statusText)); 
       }; 
} 

ウェブAPI:あなたが設定をコメントし、ちょうどペイロードとしてReportModelを送ることができるように

public IHttpActionResult GetReport(ViewReportModel objViewRepotModel) 
    { 
     try 
     { 
      //Code here 
     } 
     catch (Exception ex) 
     { 
      return NotFound(); 
     } 
    } 
+0

'$ http.post( 'api/Project/GetReport'、config)'のように 'configオブジェクト'と投稿を使わないのはなぜですか? – anoop

+0

質問を書くときは、できるだけ小さなコードを使用して、同じ問題が発生します。問題に関連していない問題を取り除く。 – georgeawg

答えて

0

$httpは、デフォルトの型としてJSONを取ります。

//var config = { 
// headers: { 'Content-Type': 'application/json;charset=utf-8' }, 
// datatype: JSON, 
// data: JSON.stringify(ReportModel) 
//}; 


var payload = {reportModel : ReportModel}; 
result = $http.post('api/Project/GetReport', payload) 
      .then(function (response) { 
       result = response.data; 
       }) 
      }, function (response) { 
       alert('Failed ' + JSON.stringify(response.statusText)); 
      }; 

また、私はより良いセキュリティ上の理由から、ここで$http.Postを使用するためにあなたをお勧めしたいです。

+0

私はすでにconfigオブジェクトを使用していません。あなたが見せてくれた方法を通り過ぎると、私はクエリ文字列の中のparamsも見ていません。 – Aneez

+0

Web apiの基本パスを使用していないので、 // localhostxxx/api/Project/GetReport' – Sajal

+0

また、ペイロードの送信の変更を参照してください。 – Sajal

関連する問題