2017-06-11 10 views
0

私の問題は角度のポストに関連しています。クライアントと一緒に送信されたデータは常に空です( )。しかし、フィドラーは問題なしで送った。 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> 

ここで私は間違いをしますか? お願いします。

+0

サーバーのPOSTが '[FromBody]'しかし、あなた使用しているように見えるしましたデータをURI Paramsとして追加するためのシリアライザの一種で、 '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> 
関連する問題