【发布时间】:2021-11-01 13:54:40
【问题描述】:
我的看法是:
def creating_profile(request):
if request.is_ajax and request.method == "POST":
array = request.POST.get('nums')
print(request.POST.get('nums'))
else:
print('it's not ajax')
我的js:
const RegisterForm = document.forms["RegisterForm"];
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
let nums = [1, 2, 3]
RegisterForm.addEventListener('submit', () => {
$.ajax({
headers: { 'X-CSRFToken': csrftoken },
url: '/account/creating-profile/',
type: 'POST',
data: { nums: nums },
success: function (data) {
//
}
});
但它返回None print(request.POST.get('nums')) 的结果,如果有人帮助我解决这个问题,我会很高兴
【问题讨论】:
-
在 $.ajax 中你缺少
dataType: 'json' -
@TusharD 也没有
-
看起来 request.POST 将无法访问 HTTP 请求中的非表单数据(json 数据),您需要访问 reqeust.body attr 参考:docs.djangoproject.com/en/dev/releases/1.5/…try ` print(json.加载(request.body.decode(“utf-8”)))`
标签: javascript django ajax django-views