Shopify - 我可以从浏览器控制台访问 JSON 对象,但不能从主题文件访问
Shopify - I can access a JSON object from the browser console, but not from the theme file
我在我的主题上使用 GET/cart.js
调用资产来获取 JSON 对象形式的购物车内容,如下所示:
var response = jQuery.getJSON('/cart.js');
然后我尝试将对象的内容记录到控制台,如下所示(在同一个片段文件中,在调用后立即):
$( document ).ready(function() {
console.log(response.responseJSON.items[0]);
});
我在控制台中得到一个 TypeError: response.responseJSON is undefined
。如果我尝试在控制台 (console.log(response.responseJSON.items[0]);
) 中再次记录相同的内容,它会工作并打印购物车的第一项。
我尝试使用 while
循环检查对象,直到它不再 undefined 为止,我还尝试仅在页面完全加载后才进行控制台日志记录。
为了清楚起见,这些是我的代码片段的全部内容:
var response = jQuery.getJSON('/cart.js');
$( document ).ready(function() {
console.log(response.responseJSON.items[0]);
});
如何直接从我的代码片段文件中完成这项工作?
编辑:我应该提到对象已经被解析,所以这不是它给我 undefined 的原因。我确实只是为了它而尝试解析它,但它给了我另一个错误。
编辑 2:这是我从电话中得到的 JSON:
{
"readyState": 4,
"responseText": "{\"token\":\"4472c3d68931e8fe2bff0afcca67a188\",\"note\":null,\"attributes\":{},\"original_total_price\":39800,\"total_price\":39800,\"total_discount\":0,\"total_weight\":0.0,\"item_count\":2,\"items\":[{\"id\":16640068878400,\"properties\":null,\"quantity\":2,\"variant_id\":16640068878400,\"key\":\"16640068878400:94cf8752e20f28a3f675ee10f8e5cc72\",\"title\":\"Compleu Abby - 60\",\"price\":19900,\"original_price\":19900,\"discounted_price\":19900,\"line_price\":39800,\"original_line_price\":39800,\"total_discount\":0,\"discounts\":[],\"sku\":\"2558\",\"grams\":0,\"vendor\":\"33 Aya\",\"taxable\":true,\"product_id\":1710662484032,\"gift_card\":false,\"url\":\"\/products\/compleu-abby?variant=16640068878400\",\"image\":\"https:\/\/cdn.shopify.com\/s\/files\/1\/0087\/2500\/4352\/products\/61_b38d4463-58b7-4569-bf0c-71b59fcb6e28.jpg?v=1544168500\",\"handle\":\"compleu-abby\",\"requires_shipping\":true,\"product_type\":\"\",\"product_title\":\"Compleu Abby\",\"product_description\":\"Compus din bluză dantelă cu tricot, eșarfă și pantalon tricot.\n*eșarfa nu este inclusă în preț, este oferită cadou și poate varia în funcție de stocul disponibil\",\"variant_title\":\"60\",\"variant_options\":[\"60\"]}],\"requires_shipping\":true,\"currency\":\"RON\"}",
"responseJSON": {
"token": "4472c3d68931e8fe2bff0afcca67a188",
"note": null,
"attributes": {},
"original_total_price": 39800,
"total_price": 39800,
"total_discount": 0,
"total_weight": 0,
"item_count": 2,
"items": [
{
"id": 16640068878400,
"properties": null,
"quantity": 2,
"variant_id": 16640068878400,
"key": "16640068878400:94cf8752e20f28a3f675ee10f8e5cc72",
"title": "Compleu Abby - 60",
"price": 19900,
"original_price": 19900,
"discounted_price": 19900,
"line_price": 39800,
"original_line_price": 39800,
"total_discount": 0,
"discounts": [],
"sku": "2558",
"grams": 0,
"vendor": "33 Aya",
"taxable": true,
"product_id": 1710662484032,
"gift_card": false,
"url": "/products/compleu-abby?variant=16640068878400",
"image": "https://cdn.shopify.com/s/files/1/0087/2500/4352/products/61_b38d4463-58b7-4569-bf0c-71b59fcb6e28.jpg?v=1544168500",
"handle": "compleu-abby",
"requires_shipping": true,
"product_type": "",
"product_title": "Compleu Abby",
"product_description": "Compus din bluză dantelă cu tricot, eșarfă și pantalon tricot.\n*eșarfa nu este inclusă în preț, este oferită cadou și poate varia în funcție de stocul disponibil",
"variant_title": "60",
"variant_options": [
"60"
]
}
],
"requires_shipping": true,
"currency": "RON"
},
"status": 200,
"statusText": "OK"
}
您应该在发出请求时传递回调(请参阅 jQuery getJSON docs 中的第一个示例),而不是假设它已完成并使用 jqXHR 对象:
$(document).ready(function() {
$.getJSON('/cart.js', function (data ) {
console.log(data);
});
});
有关 Javascript 中异步请求的更多信息,请参阅此 excellent answer
我在我的主题上使用 GET/cart.js
调用资产来获取 JSON 对象形式的购物车内容,如下所示:
var response = jQuery.getJSON('/cart.js');
然后我尝试将对象的内容记录到控制台,如下所示(在同一个片段文件中,在调用后立即):
$( document ).ready(function() {
console.log(response.responseJSON.items[0]);
});
我在控制台中得到一个 TypeError: response.responseJSON is undefined
。如果我尝试在控制台 (console.log(response.responseJSON.items[0]);
) 中再次记录相同的内容,它会工作并打印购物车的第一项。
我尝试使用 while
循环检查对象,直到它不再 undefined 为止,我还尝试仅在页面完全加载后才进行控制台日志记录。
为了清楚起见,这些是我的代码片段的全部内容:
var response = jQuery.getJSON('/cart.js');
$( document ).ready(function() {
console.log(response.responseJSON.items[0]);
});
如何直接从我的代码片段文件中完成这项工作?
编辑:我应该提到对象已经被解析,所以这不是它给我 undefined 的原因。我确实只是为了它而尝试解析它,但它给了我另一个错误。
编辑 2:这是我从电话中得到的 JSON:
{
"readyState": 4,
"responseText": "{\"token\":\"4472c3d68931e8fe2bff0afcca67a188\",\"note\":null,\"attributes\":{},\"original_total_price\":39800,\"total_price\":39800,\"total_discount\":0,\"total_weight\":0.0,\"item_count\":2,\"items\":[{\"id\":16640068878400,\"properties\":null,\"quantity\":2,\"variant_id\":16640068878400,\"key\":\"16640068878400:94cf8752e20f28a3f675ee10f8e5cc72\",\"title\":\"Compleu Abby - 60\",\"price\":19900,\"original_price\":19900,\"discounted_price\":19900,\"line_price\":39800,\"original_line_price\":39800,\"total_discount\":0,\"discounts\":[],\"sku\":\"2558\",\"grams\":0,\"vendor\":\"33 Aya\",\"taxable\":true,\"product_id\":1710662484032,\"gift_card\":false,\"url\":\"\/products\/compleu-abby?variant=16640068878400\",\"image\":\"https:\/\/cdn.shopify.com\/s\/files\/1\/0087\/2500\/4352\/products\/61_b38d4463-58b7-4569-bf0c-71b59fcb6e28.jpg?v=1544168500\",\"handle\":\"compleu-abby\",\"requires_shipping\":true,\"product_type\":\"\",\"product_title\":\"Compleu Abby\",\"product_description\":\"Compus din bluză dantelă cu tricot, eșarfă și pantalon tricot.\n*eșarfa nu este inclusă în preț, este oferită cadou și poate varia în funcție de stocul disponibil\",\"variant_title\":\"60\",\"variant_options\":[\"60\"]}],\"requires_shipping\":true,\"currency\":\"RON\"}",
"responseJSON": {
"token": "4472c3d68931e8fe2bff0afcca67a188",
"note": null,
"attributes": {},
"original_total_price": 39800,
"total_price": 39800,
"total_discount": 0,
"total_weight": 0,
"item_count": 2,
"items": [
{
"id": 16640068878400,
"properties": null,
"quantity": 2,
"variant_id": 16640068878400,
"key": "16640068878400:94cf8752e20f28a3f675ee10f8e5cc72",
"title": "Compleu Abby - 60",
"price": 19900,
"original_price": 19900,
"discounted_price": 19900,
"line_price": 39800,
"original_line_price": 39800,
"total_discount": 0,
"discounts": [],
"sku": "2558",
"grams": 0,
"vendor": "33 Aya",
"taxable": true,
"product_id": 1710662484032,
"gift_card": false,
"url": "/products/compleu-abby?variant=16640068878400",
"image": "https://cdn.shopify.com/s/files/1/0087/2500/4352/products/61_b38d4463-58b7-4569-bf0c-71b59fcb6e28.jpg?v=1544168500",
"handle": "compleu-abby",
"requires_shipping": true,
"product_type": "",
"product_title": "Compleu Abby",
"product_description": "Compus din bluză dantelă cu tricot, eșarfă și pantalon tricot.\n*eșarfa nu este inclusă în preț, este oferită cadou și poate varia în funcție de stocul disponibil",
"variant_title": "60",
"variant_options": [
"60"
]
}
],
"requires_shipping": true,
"currency": "RON"
},
"status": 200,
"statusText": "OK"
}
您应该在发出请求时传递回调(请参阅 jQuery getJSON docs 中的第一个示例),而不是假设它已完成并使用 jqXHR 对象:
$(document).ready(function() {
$.getJSON('/cart.js', function (data ) {
console.log(data);
});
});
有关 Javascript 中异步请求的更多信息,请参阅此 excellent answer