[英]How to change the keys of a dictionary from keys of another dictionary in JavaScript?
[英]How to pass dictionary from Django view to JavaScript and access dictionary value with keys
我想將字典從 Django 視圖傳遞給 JavaScript/jQuery。 在 JavaScript 中,我想使用鍵訪問我的發送字典值。 在這里,我提供了我的視圖和 JS 代碼。
看法:
def student(request):
data={
{
'name': "Joe",
'age' :15,
},
{
'name': "Jay",
'age' :16,
},
{
'name': "Jeff",
'age' :14,
},
}
return HttpResponse(data)
JS:
$(document).on("click","#button",function(e){
e.preventDefault();
$.ajax({
method:"POST",
url: "{% url 'student' %}",
data: {
csrfmiddlewaretoken:'{{csrf_token}}',
},
success: function(response){
console.log(response[0].name);
console.log(response[0].age);
},
error: function(){
console.log("error occur");
},
});
});
使用此代碼我沒有完成我想要的。
編輯:在控制台中,我檢查了響應,我得到了 TypeError,它說 unhashable type: 'dict' 。
嘗試返回 Jsonresponse,而不是返回 HTTP 響應。 返回語句將是這樣的:“返回 JsonRespnse({data})”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.