javascript - Shopify - 我可以从浏览器控制台访问 JSON 对象,但不能从主题文件访问
问题描述
我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
,并且我还尝试仅在页面完全加载后进行控制台日志记录。
为了清楚起见,这些是我的片段的全部内容:
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 文档中的第一个示例),而不是假设它已完成并使用 jqXHR 对象:
$(document).ready(function() {
$.getJSON('/cart.js', function (data ) {
console.log(data);
});
});
有关 Javascript 中的异步请求的更多信息,请参阅这个出色的答案
推荐阅读
- xdebug - 我无法使用 xdebug 配置lando
- javascript - Braintree 'payment_method_nonce' 返回空
- c - N x N井字棋检查获胜
- swift - When I bring a UITextField's input to a variable or UserDefault, the variable/UserDefault ends up as nil
- data-structures - 如何增加跳过列表,以便我们可以有效地提取跳过列表的特定段的最大值?[跳过列表未按值排序]
- corda - 安排corda流每X分钟执行一次的正确方法是什么?
- azure-active-directory - 我需要做一些特别的事情来确保 AAD Connect 同步扩展属性包含在令牌声明中吗?
- video - youtube 视频的 iframe 有助于修改它
- python - 在 Python 中使用“+”和“,”打印两个变量有什么区别?
- python - Python sqlalchemy mysql .where 日期子句