Shopify-我可以从浏览器控制台访问JSON对象,但不能从主题文件访问

时间:2018-12-11 09:50:27

标签: javascript json object undefined shopify

我正在主题上的资产中使用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"
}

1 个答案:

答案 0 :(得分:1)

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

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

有关JavaScript中的异步请求的更多信息,请参见此excellent answer