2016-05-12 4 views
-3

Я попытался опубликовать форму data.but, но это не сработало. Ниже мой код:Как отправить данные с помощью 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

, пожалуйста, укажите точную проблему. does_it не работает_ означает, что данные формы не были получены на другом конце или произошла ошибка? –

+0

Я получил ответ. ошибка связана с некоторыми ошибками в моем html. –

ответ

0

Try для использования $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 

поэтому вам нужно установить тип содержимого, как

Content-Type: application/json 

вам нужно вызвать функцию на нг представить

В сервере вам нужно разобрать JSON как данные

Смежные вопросы