我的问题与角度帖子有关。我的客户端发送的数据始终为空 - null。但小提琴手发送没有问题。AngularJS针对WebApi的发布数据
的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; }
}
}
我Base用户等级:业务层
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”是空的。 – Claies
将引号添加到$ httpParamSerializerJQLike返回的数据是什么原因?你可以尝试一次删除这些。你可以直接将数据添加到url对象。? – Naruto