2015-12-26 82 views
2

我试图从我的网页上发布数据到我的RegistrationController Api。我在提交表单时看到它,但是当我在WebApi上打断点时,参数'pilot'为空。有人能告诉我,我需要做什么才能让数据从网页传递到Registration ApiController?将JSON对象传递给WebApi

我看到这个post,但它似乎并没有为我工作。这post,这是接近我的问题,提到该模型需要注释,但仍然没有为我工作。

按钮CSHTML页:

<button id="submitRegistration" class="btn btn-lg btn-primary btn-block" 
    type="submit">Submit</button> 

角这个RegistrationController:

<script type="text/javascript"> 
'use strict'; 

var sampleApp = angular.module('RegistrationApp', []); 

sampleApp.controller('RegistrationController', function ($scope, $http) { 
    var registrationData = { 
     "firstname": $scope.firstname, 
     "lastname": $scope.lastname, 
     "faaNumber": $scope.faaNumber 
    }; 

    $('#submitRegistration').click(function() { 

     registrationData = { 
      FirstName: $scope.firstname, 
      LastName: $scope.lastname, 
      FAANumber: $scope.faaNumber 
     }; 

     // When I hit this debugger, the data is good. 
     debugger; 
     $.post("api/registration", 
       JSON.stringify(registrationData), 
       function (value) { 
       $('#registrationMessage').append(value); 
       }, 
       "json" 
     ); 
    }); 
}); 
</script> 

的WebAPI这个RegistrationController类:

public class RegistrationController : ApiController 
{ 
// *********** Pilot is null when the breakpoint hits here ************ 
public HttpResponseMessage Post([FromBody]PilotModel pilot)  
{ 
    this.RegisterPilot(pilot); 

    var response = Request.CreateResponse<PilotModel>(HttpStatusCode.Created, pilot); 

    return response; 
} 

private string RegisterPilot(PilotModel pilot) 
{ 
    var result = string.Empty; 

    ... 

    return result; 
} 
} 

驾驶员模型:

[DataContract] 
public class PilotModel 
{ 
    [DataMember] 
    public string FirstName { get; set; } 

    [DataMember] 
    public string LastName { get; set; } 

    [DataMember] 
    public string FAANumber { get; set; } 
} 

回答

1

将只需要一分钟后,试试这个:

$.post("api/registration", 
       registrationData, 
       function (value) { 
       $('#registrationMessage').append(value); 
       }, 
       "json" 
     ); 
+1

删除JSON.stringify()做到了。我以为我需要那个。我也看到我没有DataContract或DataMember属性。谢谢。 – Wannabe

2

既然你想发布您的角码的数据,你应该使用$http服务,这将需要设置适当的内容类型的护理如所须。看起来你已经在向你的控制器注入$ http服务了。

var model = { FirstName: "Shyju", 
       LastName: "Happy" 
       FAANumber: "3454353" }; 

$.http("api/registration",model) 
    .then(function(response) { 
     var result=response.data; 
     console.log(result); 
    }, function(response) { 
     //error happened. Inform the user 
    }); 

理想情况下,您应该将http调用移动到数据服务并将该数据服务注入角度控制器。

如果你仍然想使用jQuery ajax(我不推荐),你可以将你的对象转换为json字符串并指定contentType属性为"application/json"。即使您的模型具有复杂的导航属性,该方法也可以工作。

$.ajax({ 
     type: "POST", 
     data :JSON.stringify(model), 
     url: "api/registration", 
     contentType: "application/json" 
    }); 

看看this answer覆盖几乎所有使用AJAX将数据发送到网络API的使用情况。

+1

是的,Angular已经使API调用变得简单。无需通过jQuery来做到这一点。 – Ellesedil

+0

请注意,您可能认为并非所有标头实际上都是由$ http设置的。最好查看一下chrome工具或者其他的默认头文件。新的Asp.Net 5框架在处理AJAX和重定向方面存在一些问题。 – hally9k

+0

@shyju,谢谢你的回复。我已根据您的建议进行了更改,这很好。我也会考虑将http调用转换为数据服务。在我尝试学习的时候,一次只做一点点工作。 – Wannabe