Ajax POST 请求 Shopify 的 /cart/add.js 总是返回错误回调函数
Ajax POST request to Shopify's /cart/add.js always returning on error callback function
$.ajax({
type: 'POST',
url: '/cart/add.js',
data: {
quantity: 1,
id: form_data
},
success: function(response) {
console.log('in success');
var url = $("#af-btn").attr("href");
window.location.href = $("#af-btn").attr("href");
},
error: function(response) {
console.log(response + "in error");
var url = $("#af-btn").attr("href");
//window.location.href = $("#af-btn").attr("href");
},
complete: function(response) {
console.log(response);
}
});
以上是我的AJAX请求。下面是响应对象。
添加到购物车以显示匿名行为,有时会添加产品,有时不会添加产品。想不通。有什么想法吗?
下面是 form_data
我如何检索它的值,
var form_data = $('form[action="/cart/add"]').find('select[name="id"]').find(":selected").val();
标准 AJAX 调用在一个实例中也为我抛出错误(甚至认为该产品已包含在购物车中)。
所以我使用他们的 API 进行 AJAX 调用。这是所有函数的 link:http://mayert-douglas4935.myshopify.com/pages/api
例如,添加项目很容易:
Shopify.addItem($productVal, $productQTY, function(){
// Do Something here once the product is added
console.log('Success!!!')
});
PS:
请记住,您将需要包含此脚本才能访问调用:
{{ 'api.jquery.js' | shopify_asset_url | script_tag }}
这就是我在 Shopify 应用程序中使用 Shopify Ajax Api 的解决方法,其脚本是通过 Shopify Admin API ScriptTag 添加的。
1- 加载 Shopify jQuery Ajax Api 包装器
function loadShopifyAjaxApiScript()
{
var script = document.createElement("script");
script.rel = "text/javascript";
script.src = "https://cdn.shopify.com/s/assets/themes_support/api.jquery-0ea851da22ae87c0290f4eeb24bc8b513ca182f3eb721d147c009ae0f5ce14f9.js";
document.getElementsByTagName("head")[0].appendChild(script);
}
2- 然后像下面这样使用它:
Shopify.addItem(variantId , 1 , function () {
console.log('success');
});
$.ajax({
type: 'POST',
url: '/cart/add.js',
data: {
quantity: 1,
id: form_data
},
success: function(response) {
console.log('in success');
var url = $("#af-btn").attr("href");
window.location.href = $("#af-btn").attr("href");
},
error: function(response) {
console.log(response + "in error");
var url = $("#af-btn").attr("href");
//window.location.href = $("#af-btn").attr("href");
},
complete: function(response) {
console.log(response);
}
});
以上是我的AJAX请求。下面是响应对象。
添加到购物车以显示匿名行为,有时会添加产品,有时不会添加产品。想不通。有什么想法吗?
下面是 form_data
我如何检索它的值,
var form_data = $('form[action="/cart/add"]').find('select[name="id"]').find(":selected").val();
标准 AJAX 调用在一个实例中也为我抛出错误(甚至认为该产品已包含在购物车中)。
所以我使用他们的 API 进行 AJAX 调用。这是所有函数的 link:http://mayert-douglas4935.myshopify.com/pages/api
例如,添加项目很容易:
Shopify.addItem($productVal, $productQTY, function(){
// Do Something here once the product is added
console.log('Success!!!')
});
PS:
请记住,您将需要包含此脚本才能访问调用:
{{ 'api.jquery.js' | shopify_asset_url | script_tag }}
这就是我在 Shopify 应用程序中使用 Shopify Ajax Api 的解决方法,其脚本是通过 Shopify Admin API ScriptTag 添加的。
1- 加载 Shopify jQuery Ajax Api 包装器
function loadShopifyAjaxApiScript()
{
var script = document.createElement("script");
script.rel = "text/javascript";
script.src = "https://cdn.shopify.com/s/assets/themes_support/api.jquery-0ea851da22ae87c0290f4eeb24bc8b513ca182f3eb721d147c009ae0f5ce14f9.js";
document.getElementsByTagName("head")[0].appendChild(script);
}
2- 然后像下面这样使用它:
Shopify.addItem(variantId , 1 , function () {
console.log('success');
});