如何在Django中正确获取提取请求
我开始学习Django。
的按钮时,可以调用获取
button = document.querySelector('.button')
button.addEventListener('click', function(){
fetch('http://127.0.0.1:8000/test_fetch/',
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
"X-Requested-With": "XMLHttpRequest",
"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"
}
})
})
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button class="button">hello</button>
{{ ajax }}
<script src="{% static 'train_app/js/test_fetch.js' %}"></script>
</body>
class TestFetch(TemplateView):
template_name = 'train_app/test_fetch.html'
def get(self, request):
ajax = request.headers
data = {
'name': 'Yura',
'age': 20,
'list': [1,2,3,4],
'ajax': ajax
}
return render(request, self.template_name, context=data)
我有JS代码,当我单击“尝试使用request.meta and request.headers” 。但是他们仅返回我的信息,只需涉及第一个获得的请求,该请求是由urls.py引起的。
我如何获取这是提取请求及其内容类型属性的信息。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在'http://127.0.0.1:8000/test_fetch/'的方法中,我认为您定义了渲染函数。
因此,如果您想通过获取方法获取数据,则应定义新的API。
例如
views.py
urls.py
test_fetch.js
jsonresponse
什么是{%url%}? /a>
In get method of 'http://127.0.0.1:8000/test_fetch/', I think you defined a render function.
So, if you want to get data through get method, you should define new api.
For example
views.py
urls.py
test_fetch.js
What is the JsonResponse
What is {% url %}?