【问题标题】:convert http get to post in angularjs将 http get 转换为在 angularjs 中发布
【发布时间】:2016-12-20 13:07:15
【问题描述】:

出于安全原因,我正在将现有的 http.get 请求转换为 http.post 请求。 即使我编写的代码正在运行,我认为它不是很健壮。有没有更好的方法来定义数据对象?

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

示例代码如下。

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

转换为

$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);
});

有没有更好的方法在发布请求中定义数据?

后端代码如下

#!/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

【问题讨论】:

  • 使用$resource而不是$http,有一个好方法可以让工厂使用服务

标签: python angularjs post get


【解决方案1】:

您可以使用$httpParamSerializer 将您的对象转换为添加到有效负载的字符串。有一个特定的行为类似于 jQuery,我相信这就是您正在寻找的。​​p>

例子:

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

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

});

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    相关资源
    最近更新 更多