【发布时间】:2016-07-22 05:53:30
【问题描述】:
这可能是一个愚蠢的问题,但我无法正确回答。我已经搜索了所有关于 stackoverflow 的问题,当然还有谷歌。
所以,我在我的 webapi 上得到了这个类
public partial class TaskList
{
public int id { get; set; }
public string task { get; set; }
}
然后是我的 webapi 控制器
[HttpPost]
public HttpResponseMessage Post([FromBody] TaskList tasklistModel)
{
return Request.CreateResponse(HttpStatusCode.OK, tasklistModel);
}
我的 angularJS 控制器
angular.module('ReminderDude.controllers', [])
.controller('HomeController', function($scope, dudeService) {
$scope.CreateNewTask = function(tasklistModel){
tasklistModel.id = '1';
dudeService.store({
data: JSON.stringify(tasklistModel)
}).then(function(resp) {
console.log(resp);
},function(err) {
console.error('Controller: Error', err);
});
}
})
我的 angularJS 服务
angular.module('ReminderDude.services', [])
.factory('dudeService', function($http) {
var baseUrl = 'http://localhost:1142/api/values';
return {
store: function (tasklistModel){
return $http({
url: baseUrl,
method: 'POST',
data: tasklistModel,
headers: {
"Content-Type": "application/json"
}
});
}
};
})
最后,HTML
<ion-view view-title="Reminder Dude">
<ion-content class="padding">
<form ng-Submit="CreateNewTask(model)">
<div class="list card">
<div class="item item-divider">Hello! Please input your task below</div>
<div class="item item-input">
<input type="text" ng-model="model.task" placeholder="Write Here" />
</div>
</div>
<center>
<button type="submit" class="button button-positive button-large">Save</button>
</center>
</form>
</ion-content>
</ion-view>
我在我的 webapi 控制器上设置了一个断点,它命中但我传递的模型为空。这是我的 webapi 控制器返回的数据:
Object {data: Object, status: 200, config: Object, statusText: "OK"}
config: Object
data: Object
headers: Object
method: "POST"
transformRequest: Array[1]
transformResponse : Array[1]
url : "http://localhost:1142/api/values"
__proto__ :
Object: data
Object:
id: 0
task: null
__proto__ :
Object
headers: function(name)
status: 200
statusText: "OK"
__proto__ :
Object
这是我尝试过的列表:
- 在我的 webapi 控制器上使用或删除 [FromBody] 标签
- 在将我的数据发送到 webapi 控制器之前,在我的 angularJS 控制器上使用或删除 JSON.stringify
- 将“public HttpResponseMessage Post”更改为“public TaskList Post”
- console.log 从我的 angularJS 控制器发送的数据:{"task":"testTask","id":"1"}
为什么它没有正确发送到我的 webapi? 任何答案和帮助将不胜感激。
仅供参考:我认为 CORS 不是问题,我已启用它。因为如果没有启用 CORS,它就不会到达断点,不是吗?如果是CORS相关问题,请指正。
谢谢
更新:
经过与@Mahesh Chand 的长时间讨论,我已成功将数据发送到我的 webapi 控制器。这是我所做的:
将 angularjs 控制器更改为:
angular.module('ReminderDude.controllers', [])
.controller('HomeController', function($scope, dudeService) {
$scope.CreateNewTask = function(tasklistModel){
tasklistModel.id = 1;
//before
dudeService.store({
data: tasklistModel
}).then(function(resp) {
console.log('response from api:'))
console.log(resp);
//after
dudeService.store(tasklistModel).then(function(resp) {
console.log('response from api:');
console.log(resp);
//ignore the console.log, it just my code to test the response from the api
非常感谢@Mahesh Chand,感谢您注意到不必要的“数据”。 (这是一个愚蠢的错误)
@Mahesh Chand 的注意事项:就我而言,我们确实需要 ng-model 而不是 ng-bind。如果我使用 ng-bind,我不会得到我从视图中传递的值。
希望这能帮助其他和我有同样问题的人。
【问题讨论】:
标签: c# angularjs asp.net-mvc-4 asp.net-web-api ionic-framework