javascript - 监听 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))
解决方案
推荐阅读
- android - Android - EditText 在全屏风景中失去焦点
- c# - EF Core 共享事务在这里是一个好的解决方案,还是一个糟糕的设计?
- python - 如何使用 python 向 JSON 子级添加唯一 ID?
- r - 在 for 循环中运行函数并将返回的数据帧添加到 R 中的列表中
- apache-spark - 将 unix_timestamp 转换为 spark 中的普通时间戳(以秒为单位)
- python-3.x - Python脚本在目录中搜索特定文件类型,然后将它们的内容附加在一起
- memory-management - 虚拟内存效应和分页与分段之间的关系
- sockets - What is meant by record or data boundaries in the sense of TCP & UDP protocol?
- python - Python - 有效地检查列表中的每个项目与其他项目
- html - 确保图像、标题、段落和按钮保持水平,标题和段落的长度大小不同