我使用ajax传递json数组以在django中查看。但我不能得到每个json对象的值。当我调试和显示AttributeError对象没有属性“label”和“value”。请帮我解决这个问题。这是我的代码ajax和视图中的代码:
var jsonArr = [];
$('#btnSave').on('click', function () {
$('.form-group').each(function () {
debugger;
value = $(this).find("input[name='ValueRight']").val()
label = $(this).find("input[name='LabelRight']").val()
jsonArr.push({
label: label,
value: value
})
var jsonText = JSON.stringify(jsonArr);
$.ajax({
url: '{% url 'add_label_value' %}',
method: 'POST',
dataType: 'JSON',
data: {
'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
'jsonText': jsonText
},
success: function (data) {
alert(data);
}
});
})
console.log(jsonArr)
})
view.py
def add_label_value(request):
if request.method == 'POST':
try:
if request.is_ajax():
order_header = OrderHeader()
jsonText = json.loads(request.POST.get('jsonText'))
for x in jsonText:
order_header.label = x.label
order_header.value = x.value
order_header.save()
except OSError as e:
error = messages.add_message(request, messages.ERROR, e, extra_tags='add_label_value')
html = '<p>This is not ajax</p>'
return HttpResponse(html)
Python不是Javascript,字典和对象是有区别的。不能用点表示法引用字典键,必须使用字符串键。在
视图.py
相关问题 更多 >
编程相关推荐