Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Shopify-我可以从浏览器控制台访问JSON对象,但不能从主题文件访问_Javascript_Json_Object_Undefined_Shopify - Fatal编程技术网

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

Javascript Shopify-我可以从浏览器控制台访问JSON对象,但不能从主题文件访问,javascript,json,object,undefined,shopify,Javascript,Json,Object,Undefined,Shopify,我在主题的资产中使用GET/cart.js调用,以JSON对象的形式获取购物车内容,如下所示: var response = jQuery.getJSON('/cart.js'); 然后,在调用之后,我尝试将同一个代码段文件中的对象内容记录到控制台,如下所示: { "readyState": 4, "responseText": "{\"token\":\"4472c3d68931e8fe2bff0afcca67a188\",\"note\":null,\"attributes\":{

我在主题的资产中使用GET/cart.js调用,以JSON对象的形式获取购物车内容,如下所示:

var response = jQuery.getJSON('/cart.js');
然后,在调用之后,我尝试将同一个代码段文件中的对象内容记录到控制台,如下所示:

{
  "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"
}
我得到一个TypeError:response.responseJSON在控制台中未定义。如果我尝试在console.logresponse.responseJSON.items[0];,中再次记录相同的内容;,它工作并打印购物车的第一个项目

我试着用while循环检查对象,直到它不再是未定义的,我还试着在页面完全加载后才进行控制台日志记录

为清楚起见,以下是我的代码片段的全部内容:

var response = jQuery.getJSON('/cart.js');
$( document ).ready(function() {
    console.log(response.responseJSON.items[0]);
});
如何直接从代码段文件中执行此操作

编辑:我应该提到对象已经被解析,所以这不是它给我未定义的原因。我确实试着分析它只是为了它,但它给了我另一个错误

编辑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"
}

在使请求看到中的第一个示例时,您应该传递回调,而不是假设它已完成并使用jqXHR对象:

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

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

以.js结尾的文件是一个Javascript源代码文件,因此将以文本形式读取。您要做的是读取一个.json文件。因此,要么将cart.js转换为cart.json文件,要么将cart.json作为有效的json写入。或者通过更改回调函数导入脚本文件或在本地解析它。所以我们需要知道cart.js是什么样子。@Shilly说,即使所有URL都以.js结尾,响应也是一个JSON对象。我添加了我在帖子中得到的对象。你没有使用回调。使用回调,您的数据将作为响应提供。项[0]。如前所述,在解析整个请求之前,您将记录该请求,因此您正在寻找一个还不存在的responseJSON属性。只需按照链接文档中的示例代码进行操作。