Shopify 中返回了错误的 JSON 数据
Wrong JSON Data being returned within Shopify
我创建了一个 Shopify 替代模板,该模板 returns 自定义 JSON 集合中的产品,以便我可以将自定义元字段存储在对象中以在 jQuery 中使用。
我已经设置了我的 AJAX GET 调用和正确的 URL,但是当我去测试 GET 调用的响应时,而不是返回自定义 JSON根据提供的 URL,它只是返回标准的 Shopify 集合 JSON,您可以从 (store_url)/collections/(collection_name).json 中看到.
为什么不从 URL (store_url)/collections/(collection_name)?view=filter-json[=14 返回响应=]
(filter-json 是返回自定义 JSON 的液体模板)
var coll_url = window.location.href;
$.ajax({
type: 'GET',
url: coll_url,
data: {
'view': 'filter-json'
},
dataType: "json",
success: function(res){
console.log(res);
console.log(this.url);
},
error: function(status){
alert(status);
}
})
Image of a (collection_url)?view=filter-json link
Image of returned console log upon action on the page
在这种情况下,Shopify 正在 smarter-than-thou 并为您提供他们认为您想要的数据,而不是您实际想要的值。
每当您对产品页面进行 AJAX 调用并期望 'application/json' 的 response-type 时,如果您忘记结束页面请求,Shopify 将有助于 'correct' 您的疏忽使用 '.js' 或 '.json' 和 return 该产品的默认 JSON object。通过在您的请求中将 dataType
设置为 json
,jQuery 有助于自动设置这些请求 headers,因此最终结果是所有这些有用的行为都被完全误解了您的要求。
要访问您的自定义端点,您需要在不使用 json
类型的情况下请求它以将自定义页面的内容检索为纯文本,然后使用 JSON.parse
将响应数据转换为可用 object.
祝你好运!
我创建了一个 Shopify 替代模板,该模板 returns 自定义 JSON 集合中的产品,以便我可以将自定义元字段存储在对象中以在 jQuery 中使用。
我已经设置了我的 AJAX GET 调用和正确的 URL,但是当我去测试 GET 调用的响应时,而不是返回自定义 JSON根据提供的 URL,它只是返回标准的 Shopify 集合 JSON,您可以从 (store_url)/collections/(collection_name).json 中看到.
为什么不从 URL (store_url)/collections/(collection_name)?view=filter-json[=14 返回响应=]
(filter-json 是返回自定义 JSON 的液体模板)
var coll_url = window.location.href;
$.ajax({
type: 'GET',
url: coll_url,
data: {
'view': 'filter-json'
},
dataType: "json",
success: function(res){
console.log(res);
console.log(this.url);
},
error: function(status){
alert(status);
}
})
Image of a (collection_url)?view=filter-json link
Image of returned console log upon action on the page
在这种情况下,Shopify 正在 smarter-than-thou 并为您提供他们认为您想要的数据,而不是您实际想要的值。
每当您对产品页面进行 AJAX 调用并期望 'application/json' 的 response-type 时,如果您忘记结束页面请求,Shopify 将有助于 'correct' 您的疏忽使用 '.js' 或 '.json' 和 return 该产品的默认 JSON object。通过在您的请求中将 dataType
设置为 json
,jQuery 有助于自动设置这些请求 headers,因此最终结果是所有这些有用的行为都被完全误解了您的要求。
要访问您的自定义端点,您需要在不使用 json
类型的情况下请求它以将自定义页面的内容检索为纯文本,然后使用 JSON.parse
将响应数据转换为可用 object.
祝你好运!