AngularJS为WebApi发布数据

时间:2017-06-11 15:14:12

标签: c# html angularjs cordova asp.net-web-api

我的问题与角柱有关。我与客户发送的数据总是空的 - 空值。但是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>

我在哪里弄错了? 请帮帮我。

3 个答案:

答案 0 :(得分:0)

我已经尝试了所有这些,没有任何改变。 (JSON.stringify包括)

   <script>
      var app = angular.module('a', []);
        app.controller('b', function ($scope, $http) {

            $scope.Ekle = function () {


                var data =({ KullaniciAdi: $scope.username, Sifre: $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,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>

答案 1 :(得分:0)

对我来说,看起来你以不正确的方式添加了身体数据。 尝试更改$ http设置并在数据部分中定义正文

$http({
    method: 'POST',
    url: 'http://localhost:51975/api/User/KullaniciEkle',
    headers: {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
    },
    data: JSON.stringify(data),
}).
success(function (data) {
    alert("Oldu");
}).
error(function (message, status) {
    console.log(message);
});

答案 2 :(得分:0)

我做到了! 谢谢你的一切。 运行代码:

https://docs.microsoft.com/en-us/aspnet/core/security/cors

我的控制器:

 [EnableCors(origins: "*", headers: "*", methods: "*")]
 public class UserController : ApiController

我的客户发布表单脚本:

<script>
      var app = angular.module('a', []);
        app.controller('b', function ($scope, $http) {

            $scope.Ekle = function () {

                var veri = { KullaniciAdi: 'test', Sifre: 'test2' };
                var adres = 'http://localhost:51975/api/User/KullaniciEkle';
                var req = {
                    method: 'POST',
                    url: adres,
                    data: JSON.stringify(veri),
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json',
                 },}

                $http(req).then(function () {
                    alert("oldu");

                }, function () {

                    console.log(veri);
                    alert("olmadı");

                    });
            }

        });

    </script>
相关问题