2016-05-12 8 views
-3

Ich habe versucht, die Formulardaten zu posten. Aber es hat nicht funktioniert. Unten finden Sie meine Code:Wie poste ich Daten mit 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; 
      } 
      }); 
     }; 
    }); 

Kann mir jemand auf diesem helfen?

+0

Anstatt .success() zu verwenden, verwenden Sie .then() –

+0

, geben Sie bitte das genaue Problem an. tut _it nicht funktioniert_ bedeutet, dass die Formulardaten nicht am anderen Ende empfangen wurden oder ein Fehler aufgetreten ist? –

+0

Ich habe die Antwort bekommen. Der Fehler ist auf einige Fehler in meinem HTML zurückzuführen. –

Antwort

0

Versuch analysieren müssen zu verwenden $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 

so müssen Sie sich als Inhaltstyp festlegen

Content-Type: application/json 

müssen Sie die Funktion auf ng nennen

In Server senden Sie die json als Daten

Verwandte Themen