【问题标题】:Why won't my Angular front-end pass correct $http.get parameters to Django back-end?为什么我的 Angular 前端不能将正确的 $http.get 参数传递给 Django 后端?
【发布时间】:2017-01-15 22:44:54
【问题描述】:

我有一个由 Angular/Javascript 编写的前端客户端和一个 Python/Django 编写的后端服务器组成的 Web 应用程序。

我正在尝试从客户端向服务器发出一个简单的 http GET 请求,但它不工作。它失败的原因是由于某些未知原因,参数未正确传递。

这是在我的客户端上调用的代码行:

  $http.get(API + '/api/getProfile', {'profileId':5}).then(console.log('A'), console.log('B'));

以下是我服务器上的相关代码行:

class GetProfileAPI(views.APIView):
    def get(self, request, *args, **kwargs):
        logger.info("request.get_full_path() = %s" % request.get_full_path())
        profile_id_arg = request.GET.get('profileId')
        logger.info("profile_id_arg = %s (%s)" % (profile_id_arg, type(profile_id_arg)))
        # Do something here if profile_id_arg is not None.

当我运行这段代码时,我希望服务器端的profile_id_arg 是字符串5。但请查看我得到的输出:

request.get_full_path() = /api/getProfile
profile_id_arg = None (<type 'NoneType'>)

为什么没有正确接收profile_id,我该如何解决这个问题?代码示例会很有帮助。

【问题讨论】:

  • 可以发一下浏览器的http请求信息吗?

标签: javascript python angularjs django http-get


【解决方案1】:

HTTP GET 请求不能包含要发布到服务器的数据。但是,您可以在请求中添加查询字符串。

 $http.get(API + '/api/getProfile', {params:{'profileId':5}})
      .then(function (response) { /* */ })...

$http({ 
     url: API + '/api/getProfile', 
     method: "GET",
     params: {'profileId':5}
  });

check here1check here2

【讨论】:

  • 是的!使用密钥params 修复它。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
  • 2019-10-12
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 2021-12-23
相关资源
最近更新 更多