私の問題は角度のポストに関連しています。クライアントと一緒に送信されたデータは常に空です( )。しかし、フィドラーは問題なしで送った。 NULLデータ フィドラー - - フルデータWebApiのAngularJS掲載データ
表
WEBAPIエラー user.KullaniciAdi =である 'user.KullaniciAdi 'System.NullReferenceException'
のApache Cordovaのモバイル・クライアント型の例外をスローユーザーです。
マイサロゲートユーザー表:
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 Paramsとして追加するためのシリアライザの一種で、 'Body'は空です。 – Claies
$ httpParamSerializerJQLikeが返すデータに引用符を追加する理由は何ですか?それらを取り除くことで一度試すことができますか?そして、あなたはURLオブジェクトに直接データを追加することができます。 – Naruto