Angular JS不向Web Api发送数据

时间:2016-10-27 10:37:54

标签: angularjs web asp.net-web-api

enter image description here

enter image description here

enter image description here

Angular Post方法不向web api Controller发送数据。 $ http调用web api但是为null。

4 个答案:

答案 0 :(得分:4)

首先定义(初始化)$FirstNameLastNameEmailNumber为空。

$scope.FirstName = null;
$scope.LastName = null;
$scope.Email = null;
$scope.Number = null;

或者您可以使用$scope.user作为

$scope.user = {
    id : 0,
    FirstName : null,
    LastName : null,
    Email : null,
    Password : null
};

并将ng-model作为user.FirstName

传递

修改

$scope.user功能

之外构建CreateUser
$scope.CreatUser=function(){
   $http{(...)};
}

<强> ENDS

答案 1 :(得分:1)

将$ scope.User移动到控制器范围:

app.controller('myCtrl',function($scope,$http){
   $scope.User = {
    id : 0,
    FirstName : null,
    LastName : null,
    Email : null,
    Password : null
    };

    $scope.CreatUser=function(){
      $http{(...)};
    }

})

并将ng-model作为User.FirstName传递。

答案 2 :(得分:1)

protected void Application_BeginRequest() {
  if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS"){
    Response.Flush();
  }
}

答案 3 :(得分:1)

您遇到此问题,因为您的标头请求content-type是:application/x-www-form-urlencoded无法正常运行。尝试将其更改为application/json