如何使用Angularjs发布数据?

如何使用Angularjs发布数据?,angularjs,Angularjs,我试图发布表单数据,但没有成功。 下面是我的代码: var app = angular.module('myApp', []); // Controller function and passing $http service and $scope var. app.controller('myCtrl', function($scope, $http) { // create a blank object to handle form data.

我试图发布表单数据,但没有成功。 下面是我的代码:

 var app = angular.module('myApp', []);
    // Controller function and passing $http service and $scope var.
    app.controller('myCtrl', function($scope, $http) {
      // create a blank object to handle form data.
        $scope.user = {};
      // calling our submit function.
        $scope.submitForm = function() {
        // Posting data to  file
        $http({
          method  : 'POST',
          url     : '/tokken/d/',
          data    : $scope.user, //forms user object
          headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
         })
          .success(function(data) {
            if (data.errors) {
              // Showing errors.
              $scope.errorName = data.errors.name;
              $scope.erroPassword = data.errors.password;

            } else {
              $scope.message = data.message;
            }
          });
        };
    });
有人能帮我吗

$scope.user is a JSON object not form-data 
因此,您需要将内容类型设置为

Content-Type: application/json
您需要在ng submit上调用函数

在服务器中,您需要将json解析为数据

尝试使用:


不要使用.success(),而是使用.then()请指定确切的问题。它不起作用意味着表单数据在另一端没有收到,还是发生了错误?我得到了答案。这个错误是由于我的html中的一些错误造成的。
var app = angular.module('myApp', []);
  // Controller function and passing $http service and $scope var.
  app.controller('myCtrl', function($scope, $http) {
    // create a blank object to handle form data.
    $scope.user = {};
    // calling our submit function.
    $scope.submitForm = function() {
      // Posting data to  file
      $http.post('/tokken/d/', $scope.user).then(function (data) {
        if (data.errors) {
          // Showing errors.
          $scope.errorName = data.errors.name;
          $scope.erroPassword = data.errors.password;
        } else {
          $scope.message = data.message;
        }
      });
    };
  });