c# - Posting with parameter and returning as a json response -


how post data web api , return result?

i'm trying pass in data web api, , returning result set in form of 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> 

my 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' placeholder, parameter passing in             .then(function(data) {                 $scope.dataset = data.data;             });     }); 

when navigating url: http://localhost:36059/api/samples/getsamplesbystatus?statustype=received getting following json result:

enter image description here

finally, in web api, response is:

public ihttpactionresult getsamplesbystatus(string statustype) {     var result = sample in samples                  join status in statuses                      on sample.statusid equals status.statusid                  status.statustype == statustype                  select sample;      return json(result); } 

when running poster, empty dataset:

enter image description here

i getting error:

failed load resource: server responded status of 405 (method not allowed)

after researching issue, i've installed on chrome.

enter image description here

this working when gets, issue @ hand not making difference.

how post data web api , return result?

you need specify content-type of post data in header:

content-type: application/json 

for example:

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

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -