【发布时间】:2017-11-13 02:23:35
【问题描述】:
我的问题与角度帖子有关。我与客户端发送的数据始终为空- 空值。但是 Fiddler 发送没有问题。
WebApi 出错 user.KullaniciAdi = 'user.KullaniciAdi' 抛出类型为 'System.NullReferenceException'
的异常Apache Cordova 移动客户端 - 空数据 Fiddler - 完整数据
表是用户。
我的代理用户表:
namespace DiaryRestfulService.Models.ORM
{
public class UserSurrogate
{
public int ID { get; set; }
public string Username{ get; set; }
public string Password{ get; set; }
}
}
我的基本用户类:业务层
public UserSurrogate InsertUser(UserSurrogate user)
{
Users kullanici = new Users()
{
Username= user.Username,
Password = user.Password
};
db.Users.Add(kullanici);
db.SaveChanges();
return user;
}
我的用户控制器:
[HttpPost]
public IHttpActionResult KullaniciEkle([FromBody] UserSurrogate user)
{
try
{
userornek.InsertUser(user);
return Json("succ");
}
catch (Exception)
{
return NotFound();
}
}
还有,我的客户;
<script>
var app = angular.module('a', []);
app.controller('b', function ($scope, $http, $httpParamSerializerJQLike) {
$scope.Ekle = function () {
var data =({ Username: $scope.username, Password: $scope.password });
console.log(data);
var url = {
method: 'POST',
url: 'http://localhost:51975/api/User/KullaniciEkle',
headers: {
'Content-Type': 'application/json; charset = utf-8'
}
};
$http(url, "'" + $httpParamSerializerJQLike(data)+"'").then(function (responce) {
alert("Oldu");
}, function (responce) {
console.log(responce.headers);
console.log(responce.data);
console.log(responce.status);
console.log(responce.config);
alert("Olmadı");
});
}
});
</script>
我在哪里犯了错误? 请帮帮我。
【问题讨论】:
-
您的服务器 POST 有
[FromBody],但您似乎正在使用某种序列化程序将数据添加为 URI 参数,而Body为空。 -
$httpParamSerializerJQLike返回的数据加引号的原因是什么?您可以通过删除它们来尝试一次。你可以直接将数据添加到 url 对象。?
标签: c# html angularjs cordova asp.net-web-api