获取 API post 数据未在 Django 视图中接收
Fetch API post data not receiving in Django view
我想做什么?
我正在尝试使用 fetch API 将带有数据的 post 请求发送到 Django 视图,如下所示:
javascript:
const data = {
search_text: "",
months: 6,
property_type: "all"
};
const headers = {
'Accept': 'application/json',
'Content-Type':'application/json',
'X-Requested-With':'XMLHttpRequest'
}
fetch("some-url", {method: "POST", headers: headers, body: JSON.stringify(data)})
.then((response) => response.json())
.then((data) => console.log(data));
views.py:
class MyView(View):
def post(self, request, *args, **kwargs):
print("request: ", request.POST)
# do stuff with post data
urls.py:
re_path(r"^my_view/$", login_required(csrf_exempt(MyView.as_view())), name="my_view"),
问题
当我尝试访问我的 Django 视图中的 post 数据时,我得到空的 QueryDict 并且终端的输出是这样的:
request: <QueryDict: {}>
[06/Jan/2022 06:48:19] "POST /my_app/my_view/ HTTP/1.1" 200 114
Forbidden (CSRF token missing or incorrect.): /inbox/notifications/api/unread_list/
[06/Jan/2022 06:48:22] "{"search_text":"","months":"6","property_type":"all"}GET /inbox/notifications/api/unread_list/?max=5 HTTP/1.1" 403 12291
如果您注意到最后一行 post 数据似乎已添加到终端中,为什么会这样?还有为什么我在 url 中使用 csrf_exempt
时也会被禁止访问(CSRF 令牌丢失或不正确。)?
我已经尝试查找它,但似乎没有任何效果。我们正在使用 React,我知道也可以使用 axios,但它应该可以与 fetch 一起使用 API 为什么它不起作用,请帮忙。
编辑:
即使在添加 docs 中提到的 csrf 令牌并从 url 中删除 csrf_exempt
之后,我仍然遇到同样的问题。
这是因为您的视图受到 CSRF-protection-middleware 的保护。
你有两种可能:
- 使视图免于此保护,请参阅csrf_exempt
- 将 CSRF 令牌添加到您的请求中(参见 Yevgeniy Kosmak 的回答)
好的,我正在寻找的数据似乎不在 request.POST
中,而是在 request.body
中解决了这个问题,做了以下更改:
import json
class MyView(View):
def post(self, request, *args, **kwargs):
print("request: ", json.loads(request.body))
# do stuff with post data
request.body
returns 字节字符串,因此需要使用 json.loads
将其转换为 json 以获取更多信息,请阅读 docs
我想做什么?
我正在尝试使用 fetch API 将带有数据的 post 请求发送到 Django 视图,如下所示:
javascript:
const data = {
search_text: "",
months: 6,
property_type: "all"
};
const headers = {
'Accept': 'application/json',
'Content-Type':'application/json',
'X-Requested-With':'XMLHttpRequest'
}
fetch("some-url", {method: "POST", headers: headers, body: JSON.stringify(data)})
.then((response) => response.json())
.then((data) => console.log(data));
views.py:
class MyView(View):
def post(self, request, *args, **kwargs):
print("request: ", request.POST)
# do stuff with post data
urls.py:
re_path(r"^my_view/$", login_required(csrf_exempt(MyView.as_view())), name="my_view"),
问题
当我尝试访问我的 Django 视图中的 post 数据时,我得到空的 QueryDict 并且终端的输出是这样的:
request: <QueryDict: {}>
[06/Jan/2022 06:48:19] "POST /my_app/my_view/ HTTP/1.1" 200 114
Forbidden (CSRF token missing or incorrect.): /inbox/notifications/api/unread_list/
[06/Jan/2022 06:48:22] "{"search_text":"","months":"6","property_type":"all"}GET /inbox/notifications/api/unread_list/?max=5 HTTP/1.1" 403 12291
如果您注意到最后一行 post 数据似乎已添加到终端中,为什么会这样?还有为什么我在 url 中使用 csrf_exempt
时也会被禁止访问(CSRF 令牌丢失或不正确。)?
我已经尝试查找它,但似乎没有任何效果。我们正在使用 React,我知道也可以使用 axios,但它应该可以与 fetch 一起使用 API 为什么它不起作用,请帮忙。
编辑:
即使在添加 docs 中提到的 csrf 令牌并从 url 中删除 csrf_exempt
之后,我仍然遇到同样的问题。
这是因为您的视图受到 CSRF-protection-middleware 的保护。
你有两种可能:
- 使视图免于此保护,请参阅csrf_exempt
- 将 CSRF 令牌添加到您的请求中(参见 Yevgeniy Kosmak 的回答)
好的,我正在寻找的数据似乎不在 request.POST
中,而是在 request.body
中解决了这个问题,做了以下更改:
import json
class MyView(View):
def post(self, request, *args, **kwargs):
print("request: ", json.loads(request.body))
# do stuff with post data
request.body
returns 字节字符串,因此需要使用 json.loads
将其转换为 json 以获取更多信息,请阅读 docs