python - 在 django 中读取 Ajax 发布数据
问题描述
我在 django 项目中使用Promise收到了 Ajax 请求:
var path = window.location.pathname;
fetch('/getblogs/', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({'path': path})
}).then(function (response) {
return response.json();
});
请求在js
文件中,没有表格。
我正在尝试views.py
像这样读取数据:
@csrf_exempt
def get_blogs(request):
cat_id = request.POST.get('path')
print("RESULT: " + str(cat_id))
但在输出中我得到:
RESULT: None
我在阅读帖子数据时是否遗漏了一些东西,或者我的 ajax 请求有问题?
解决方案
我想你可以这样尝试:
import json
@csrf_exempt
def get_blogs(request):
cat_id = json.loads(request.body).get('path')
print("RESULT: " + str(cat_id))
推荐阅读
- python - 如何将几个时间段存储到 MySQL 表中?
- laravel - 谷歌翻译在本地主机上运行良好,但在实时服务器上抛出“429 Too Many Requests”错误
- javascript - 获取类中的属性值
- r - 检查数据框列是否有空单元格
- python - Elasticsearch Scroll API 错误:RequestError(400, 'ElasticsearchIllegalArgumentException[Failed to decode scrollId])
- c++ - 为 XCode iOS 构建 C++ 静态库的问题
- javascript - Paypal 检查用户是否已经购买了东西
- wordpress - Apache 随机慢速图像加载
- python - 如何修复 IndexError:太多级别:索引只有 1 级,而不是 Python 中的 2 级
- python - 为 ALB 触发的多个 python lambdas 创建单个 swagger doc