我的問題與角度帖子有關。我的客戶端發送的數據始終爲空 - 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