gpt4 book ai didi

javascript - 为什么我的 Angular 前端不能将正确的 $http.get 参数传递给 Django 后端?

转载 作者:行者123 更新时间:2023-11-30 15:50:43 24 4
gpt4 key购买 nike

我有一个 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 here1check here2

关于javascript - 为什么我的 Angular 前端不能将正确的 $http.get 参数传递给 Django 后端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39383507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com