【问题标题】:AngularJS post data for WebApiAngularJS 为 WebApi 发布数据
【发布时间】: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


【解决方案1】:

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

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>

【讨论】:

    【解决方案2】:

    在我看来,您添加正文数据的方式不正确。 尝试更改 $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);
    });
    

    【讨论】:

      【解决方案3】:

      我已经尝试了所有这些,没有任何改变。 (包括 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>
      

      【讨论】:

        猜你喜欢
        • 2018-02-06
        • 1970-01-01
        • 1970-01-01
        • 2015-11-28
        • 2014-07-23
        • 2017-06-05
        • 1970-01-01
        • 2016-07-22
        • 2018-11-22
        相关资源
        最近更新 更多