2016-05-12 6 views
-3

フォームデータを投稿しようとしましたが、うまくいきませんでした。 以下は私のコードです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. 
     $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; 
      } 
      }); 
     }; 
    }); 

誰かが私に助けてくれますか?

+0

.success()を使用する代わりに、.then()を使用してください。 –

+0

正確な問題を指定してください。フォームデータが相手側で受信されなかったか、エラーが発生したことを意味しますか? –

+0

私は答えを得た。エラーは私のHTMLのいくつかの間違いのためです。 –

答えて

0

てみてJSONを解析する必要があるサーバーでは

を提出ngの上の関数を呼び出す必要が

Content-Type: application/json 

などのコンテンツタイプを設定する必要があります使用する$http.post()

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; 
     } 
     }); 
    }; 
    }); 
0
$scope.user is a JSON object not form-data 

ので、あなたは、あなたがデータ

関連する問題