首页 > 解决方案 > 监听 Ajax 调用并添加自定义参数

问题描述

我想收听所有 Shopify add.js 调用,然后在需要时添加自定义 POST 参数。

我能够听到这些调用,但不确定是否可以使用 window.fetch 添加参数

下面是代码示例 -

(function(ns, fetch) {
if (typeof fetch !== 'function') return;

ns.fetch = function() {
    const response = fetch.apply(this, arguments);

    response.then(res => {
        if ([
                `${window.location.origin}/cart/add.js`,
                `${window.location.origin}/cart/update.js`,
                `${window.location.origin}/cart/change.js`,
                `${window.location.origin}/cart/clear.js`,
            ].includes(res.url)) {
            res.clone().json().then(data => console.log(data));
        }
    });

    return response;
}

}(window, window.fetch))

标签: javascriptajaxdom-eventsshopify

解决方案


推荐阅读