[英]How to Access values from dictionary in Javascript
I am try to fetch my item_id field from model.py in django in query set form that below我正在尝试以查询集形式从 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)>]>
Here is my JS Code这是我的 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'])
};
But it print in console但它在控制台中打印
`(index):765
{items: "<QuerySet [<Item: TOWEL Pure Pineapple>, <Item: Ba…s Leopard Shoes>, <Item: Jacket Jackson
Jacket>]>"}
(index):767 undefined`
Try to convert data into a string before receiving it:尝试在接收数据之前将其转换为字符串:
x = JSON.stringify(data);
items = JSON.parse(x);
Don't forget the ';'不要忘记';'
$.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.