4

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 the beckend (Flask), I have this:

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

The problem is that request.args come empty.

1 Answer 1

9

UPDATE

After have a lot of problems with this, I solve using 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 figured 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.

Flask:

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's because request.args contains url parameters (so everything after the ?) only, request.form contains POST fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.