0

I am converting an already existing http.get request to a http.post request due to security reasons. even though the code i have written is working, i dont think it is very robust. Is there a better way to define the data object?

I would prefer sending a dictionary and reading it in the back end.

Sample code goes as below.

http.get("/ajax/validate_login.py",{params:{"email":$scope.userEmail,"password":$scope.password}}).then(function(response) {
 console.log(response);
});

is converted to

$http({ method: 'POST', url: '/ajax/validate_login.py?',
          data: 'email=' + $scope.userEmail + '&password=' + $scope.password ,
          headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function(response) {
console.log(response);
});

is there a better way to define data in post request ?

Back end code is as below

#!/usr/bin/python
import cgi
import json
import MySQLdb
import MySQLdb.cursors
import hashlib
import sys



form = cgi.FieldStorage()

email=form["email"].value
password = form["password"].value


print "Content-Type: application/json"
print
1
  • use $resource instead of $http, there is good way you can make factory to use services Commented Dec 20, 2016 at 13:10

1 Answer 1

6

You can use the $httpParamSerializer to transform your object into a string which is added to the payload. There is a particular one that acts like jQuery's which I believe is what you are looking for.

Example:

.controller(function($http, $httpParamSerializerJQLike) {
  //...

  $http({
    url: myUrl,
    method: 'POST',
    data: $httpParamSerializerJQLike(myData),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });

});

More information: https://docs.angularjs.org/api/ng/service/$httpParamSerializerJQLike

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

Comments

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.