2016-06-29 7 views
0

Wie poste ich Daten in Web-API und gebe ein Ergebnis zurück?Buchung mit Parameter und Rückgabe als JSON-Antwort

Ich versuche, Daten in Web-API übergeben und eine Ergebnismenge in Form von JSON zurückgegeben.

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

Mein Controller:

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

Wenn an die URL der Navigation: http://localhost:36059/api/Samples/GetSamplesByStatus?statustype=Received mir folgendes json Ergebnis zu erzielen:

enter image description here

schließlich in meinem Web-api, meine Antwort ist:

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

Wenn mein Plakat läuft, bekomme ich einen leeren Datensatz:

enter image description here

ich diese Störung erhalte:

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

Nach dem Problem untersucht, ich habe auf Chrom installiert.

enter image description here

arbeitet Dies ist, wenn ich einfach tun GETs, aber für die Frage auf der Hand wird es keinen Unterschied machen.

Wie poste ich Daten in Web-API und gebe ein Ergebnis zurück?

Antwort

1

Sie den Content-Type Ihrer Post-Daten im Header angeben müssen:

Content-Type: application/json 

Zum Beispiel:

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

sorry im einen Anfänger mit dem vorderen Ende aber Ihre Lösung aussieht wie jQuery, nicht eckig –

+0

Sie sind wirklich hahaha. Lesen Sie alles darüber https://docs.angularjs.org/api/ng/service/$http – Oluwafemi

+0

danke. Ich bekomme diesen Fehler http://i.imgur.com/K3vRYr2.png –