首页 > 解决方案 > 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"
}

标签: javascriptjsonobjectundefinedshopify

解决方案


您应该在发出请求时传递回调(请参阅jQuery getJSON 文档中的第一个示例),而不是假设它已完成并使用 jqXHR 对象:

$(document).ready(function() {
  $.getJSON('/cart.js', function (data ) {
    console.log(data);
  });
});

有关 Javascript 中的异步请求的更多信息,请参阅这个出色的答案


推荐阅读