[英]How to Access values from dictionary in Javascript
我正在嘗試以查詢集形式從 django 中的 model.py 獲取我的 item_id 字段,如下所示
<QuerySet [<Item: Shoulder Bag Boys Shoulder Bag (Yellow )>,
<Item: Sweeter Cotton Sweeter>,
<Item: Shirt Full Sleeves Shirt>, <Item: Jacket Jackson Jacket>,
<Item: Yellow Shoes Leopard Shoes>,
<Item: Bag Mini Cary Bag>, <Item: Coat Overcoat (Gray)>,
<Item: TOWEL Pure Pineapple>,
<Item: Coat Pure Pineapple>, <Item: TOWEL Pure Pineapple (White)>]>
這是我的 JS 代碼
$.ajax({
type: 'GET',
url: '/shopsorting/' + selected_value,
// data: formData,
encode: true
})
.done(function(data) {
items = JSON.parse(data)
console.log(items)
for (var item in items) {
console.log(item['product_id'])
};
但它在控制台中打印
`(index):765
{items: "<QuerySet [<Item: TOWEL Pure Pineapple>, <Item: Ba…s Leopard Shoes>, <Item: Jacket Jackson
Jacket>]>"}
(index):767 undefined`
嘗試在接收數據之前將其轉換為字符串:
x = JSON.stringify(data);
items = JSON.parse(x);
不要忘記';'
$.ajax({
type: 'GET',
url: '\/shopsorting\/' + selected_value,
// data: formData,
encode: true,
success: function(data) {
x = JSON.stringify(data);
items = JSON.parse(x);
console.log(items);
for (var item in items) {
console.log(item['product_id'])
};
}});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.