2015-10-17 82 views
0

我有一個角度表單,用戶輸入各種標準,然後我想傳遞給Web Api並在查詢運行後得到結果。我原本以爲這是一個「獲取」,但無法將複雜的對象傳遞給Web Api。有了一些建議之後,我使用了Post,並且能夠通過Web API中的條件運行查詢,但是我很難將結果返回到Angular。 Web Api方法運行並獲取結果。但我在數據服務中看不到結果。將Angularjs中的複雜對象傳遞到Web Api

查詢標準是多個字段而有些是列表的最佳方法是什麼?我一直沒有找到任何好的例子。

這裏是Web API方法:

[HttpPost] 公共IEnumerable的過帳([FromBody] FrequentPawnerReportCriteria條件) { 變種回購=新FrequentPawnerReport(); var result = repo.GetReport(criteria); 返回結果; }`

這裏是DataService在:

function getFrequentPawner(criteria) { 
      return $http.post("/api/FrequentPawner/Post", criteria) 
       .then (getFrequentPawnerComplete) 
       .catch(getFrequentPawnerFailed); 
      function getFrequentPawnerComplete(response) { 
       var x = response 
       return response.data.results; 
      } 
      function getFrequentPawnerFailed(error) { 
       alert("XHR failed for frequent pawner report: " + error.responseText); 
      } 
     } 

這裏是控制器代碼:

function getTopPawnerResults(criteria) { 

      return DataContext.getFrequentPawner(criteria) 
       .then(
       function (result) { 
        vm.frequentPawnerReport = result.data; 

        return vm.frequentPawnerReport; 
       }); 
     } 
+1

請顯示代碼,列出迄今爲止您嘗試過的內容。如果您爲了清晰起見添加代碼,用戶將更有可能幫助您。 – buzzsaw

+0

使用帖子或獲取不應該基於「我原本認爲這是一個」獲取「,但無法將複雜的對象傳遞到Web Api」 –

+1

真的不清楚具體問題是什麼。來自服務器的響應不應該是獲得或發佈的問題 – charlietfl

回答

0

只需使用JSON。使用JSON.stringify()將JSON對象解析爲字符串並將其發佈。同樣,從服務器返回JSON字符串,並將其分配給Angular中的變量。它會自動轉換爲JSON對象。

0

我想當你提出你的發佈請求時,你需要有一個回調函數,當你的Web Api返回時會被調用。在該回調函數中,您可以更新$ scope變量,這將使您的web ui顯示來自服務器的響應。你可以找到我的意思在這裏一個例子:https://docs.angularjs.org/api/ng/service/$http

它的要點: $http({ method: 'POST', url: '/path/to/your/web/api', function(success) { console.log('Successfully executed the api call'); $scope.response = response; // change this to match the data you are expecting from the server response }, function(failure) { console.error('There was an error'); $scope.failure = failure; // change this to match your failure response } );

0

感謝響應。該項目是Web表單和angularjs的混合。我正在遷移應用程序,並沒有注意到這個表單有一個衝突,導致一個帖子回來,並使其看起來像沒有被返回的結果。我把這個表格變成了一個單獨的項目,並且能夠得到我想要的結果。

相關問題