2016-06-29 3 views
0

어떻게 웹 API에 데이터를 게시하고 결과를 반환합니까?매개 변수가있는 게시 및 json 응답으로 반환

웹 API에 데이터를 전달하고 결과 집합을 json 형식으로 반환하려고합니다.

<div class="panel1" ng-show="tab===2"> 
    <table st-table="rowCollection" class="table table-striped" ng-controller="GetSamplesByStatus"> 
     <thead> 
      <tr> 
       <th>SampleId</th> 
       <th>Barcode</th> 
       <th>CreatedAt</th> 
       <th>CreatedBy</th> 
       <th>StatusId</th> 
     </thead> 
     <tbody> 
      <tr ng-repeat="row in dataset | limitTo:10"> 
       <td>{{row.Sample.SampleId}}</td> 
       <td>{{row.Sample.Barcode}}</td> 
       <td>{{row.Sample.CreatedAt | date:'MM/dd/yyyy'}}</td> 
       <td>{{row.Sample.CreatedBy}}</td> 
       <td>{{row.Sample.StatusId}}</td> 
      </tr> 
     </tbody> 
    </table> 
</div> 

내 컨트롤러 :

'use strict'; 
var app = angular.module('myApp', []); 

app.controller('GetAllSamplesByStatusUser', 
    function ($scope, $http) { 
     $http.get('http://localhost:36059/api/Samples/GetAllSamplesByStatusUser') 
      .then(function (data) { 
       $scope.dataset = data.data; 
      }); 
    }); 

app.controller('GetSamplesByStatus', 
    function($scope, $http) { 
     $http.post("http://localhost:36059/api/Samples/GetSamplesByStatus" + '?statustype=Received') //"received' for now it is just a placeholder, this would be the parameter which I am passing in 
      .then(function(data) { 
       $scope.dataset = data.data; 
      }); 
    }); 

URL로 이동 : http://localhost:36059/api/Samples/GetSamplesByStatus?statustype=Received를 나는 다음과 같은 JSON 결과를 얻고있다 :

enter image description here

마지막으로, 내 웹 API에, 내 대답을 is :

public IHttpActionResult GetSamplesByStatus(string statustype) 
{ 
    var result = from sample in Samples 
       join status in Statuses 
        on sample.StatusId equals status.StatusId 
       where status.StatusType == statustype 
       select sample; 

    return Json(result); 
} 
나는이 오류를 얻고있다

enter image description here

:

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

문제를 조사한 후, 나는 크롬에 설치 한

내 포스터를 실행 (3210), 나는 빈 데이터 집합을 얻을. 내가 가져 간단히 수행 할 때

enter image description here

이 작동하지만, 손의 문제에 대한 그것은 차이를 만드는되지 않습니다.

어떻게 웹 API에 데이터를 게시하고 결과를 반환합니까?

답변

1

당신은 헤더에 게시물 데이터의 콘텐츠 형식을 지정해야합니다

예를 들어
Content-Type: application/json 

:

var serializedData = $.param({name: "myname", age:24}); 

    $http({ 
     method: 'POST', 
     url: (ENDPOINT URL), 
     data: serializedData, 
     headers: { 
      'Content-Type': 'application/json' 
     }}).then(function(response) { 
       console.log(response); 
      }, function(error) { 
       console.log(error); 
      }); 
+0

죄송 메신저 프론트 엔드와 초보자하지만 솔루션 외모 jquery처럼 각도가 없습니다 –

+0

당신은 정말로 하하입니다. 그것에 대해 모두 읽어보십시오. https://docs.angularjs.org/api/ng/service/$http – Oluwafemi

+0

감사합니다. 이 오류가 발생합니다. http://i.imgur.com/K3vRYr2.png –

관련 문제