将http转换为在angularjs中发布

Har*_*sti 0 python post get angularjs

我出于安全原因将现有的http.get请求转换为http.post请求.即使我写的代码工作,我不认为它是非常强大的.有没有更好的方法来定义数据对象?

我更喜欢发送字典并在后端阅读它.

示例代码如下.

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

转换为

$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);
});
Run Code Online (Sandbox Code Playgroud)

是否有更好的方法在post请求中定义数据?

后端代码如下

#!/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
Run Code Online (Sandbox Code Playgroud)

The*_*One 6

您可以使用$ httpParamSerializer将对象转换为添加到有效内容的字符串.有一个特定的行为像jQuery,我相信你正在寻找.

例:

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

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

});
Run Code Online (Sandbox Code Playgroud)

更多信息:https://docs.angularjs.org/api/ng/service/$httpParamSerializerJQLike