2017-06-11 102 views
0

我的问题与角度帖子有关。我的客户端发送的数据始终为空 - 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> 

我在哪里犯错? 请帮助我。

+0

服务器POST已'[FromBody]',但你似乎是使用某些序列化器将数据添加为URI参数,而“Body”是空的。 – Claies

+0

将引号添加到$ httpParamSerializerJQLike返回的数据是什么原因?你可以尝试一次删除这些。你可以直接将数据添加到url对象。? – Naruto

回答

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> 
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); 
}); 
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>