Sending a POST request using Angularjs and getting parameters in a flash drive

I have this code in AngularJS:

myApp.controller('LoginCtrl', function($scope, $http){
  $scope.formData = {};

  $scope.doLogin = function(url, pass){
    $http({
      url: url,
      method: "POST",
      data: $.param($scope.formData)
    }).success(function(data) {
      console.log(data)
    });
  }
});

And in beckend (Flask) I have the following:

def user_authenticate():
    login = request.args.get('login')
    password = request.args.get('password')
    print login, password

The problem is that request.args is empty.

+4
source share
1 answer

UPDATE

After I had a lot of problems, I decide to use another Stackoverflow answer. So, I got this code:

ANGULARJS

$scope.doLogin = function(url, pass){
    $http({
      url: url,
      method: "POST",
      headers: { 'Content-Type': 'application/json' },
      data: JSON.stringify(data)
    }).success(function(data) {
      console.log(data)
    });
 }

FLASK

def view_of_test():
    post = request.get_json()
    param = post.get('param_name')

OLD VERSION

I just thought that I need to change my code to this:

AngularJS:

myApp.controller('LoginCtrl', function($scope, $http){
  $scope.formData = {};

  $scope.doLogin = function(url, pass){
    $http({
      url: url,
      method: "POST",
      data: $.param($scope.formData),
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).success(function(data) {
      console.log(data)
    });
  }
});

Just include the Content-Type header.

Infusion:

def user_authenticate():
    login = request.form.get('login')
    password = request.form.get('password')
    print login, password

Instead of using request.args, use request.form.

+9
source

Source: https://habr.com/ru/post/1537209/


All Articles