当用户点击按钮时,我试图通过Django中的AJAX POST发送一个带有数组的对象。我按照Django docs上的说明进行了操作。我试图在视图中使用logger.debug()
查看数组中的特定指标,但无法访问内容。如何访问数组中的特定元素?
我的AJAX ......
var tags_selected = [1,2,3,4]
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
$("#filter").click(function(){
$.ajax({
type: 'POST',
url: "/pensieve/filtertags",
data: {'tags': tags_selected},
success: function(res){
console.log(res)
},
dataType: "json"
});
})
我的观点......
def filter_tags(request):
logger.debug(request.POST)
return HttpResponse(json.dumps({'response': 111}), content_type="application/json")
我的DEBUG登录终端...
DEBUG - 2016-05-06:17:15:50 - <QueryDict: {u'tags[]': [u'142', u'122', u'138']}>
答案 0 :(得分:3)
您可以使用printMessage
方法获取tags[]
数组的值。
示例:
getlist()