为什么我的Angular前端不传递正确的$http ?获取参数到Django后端

Why won't my Angular front-end pass correct $http.get parameters to Django back-end?

本文关键字:http 获取 参数 后端 Django 前端 Angular 我的 为什么      更新时间:2023-09-26

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

我试图做一个简单的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 GET请求不能包含要发送到服务器的数据。但是,您可以在请求中添加查询字符串。

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

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

check here1 and check here2