angular - Angular 的 HttpRequest 对象可以保存在本地存储中吗?
问题描述
在我的 Angular/Ionic 应用程序中,我想将 HttpRequest 对象保存到本地存储(使用 Ionic 的本机存储),在某些特定条件下从 http 拦截器调用:
public addRequestToQueue(request) {
let queuedRequests = [];
this.storage.get('queuedRequests').then((requests) => {
if(requests) {
queuedRequests = requests;
}
queuedRequests.push(request);
this.storage.set('queuedRequests', queuedRequests);
});
}
但我在控制台中收到错误:
Uncaught (in promise): DataCloneError: Failed to execute 'put' on 'IDBObjectStore': function () {
_this.headers = new Map();
Object.keys(headers).forEach(function ...<omitted>... } could not be cloned.
Error: Failed to execute 'put' on 'IDBObjectStore': function () {
_this.headers = new Map();
Object.keys(headers).forEach(function ...<omitted>... } could not be cloned.
如何将它们保存在本地存储中以备后用?可能吗?也许应该使用某种序列化方法?
解决方案
更新
在研究了一下这个错误后,我发现通常有一个框架或工具向 Array 原型添加属性,这使得它无法复制keys
. 您可以参考以下关于 pouchdb 的github 讨论,其中用户遇到了类似的错误。
只是为了验证没有其他任何东西干扰您的存储,请尝试对请求进行编码并将其放入:
public addRequestToQueue(request) {
this.storage.set('request', JSON.stringify(request));
});
这个
推荐阅读
- oracle - 尝试创建对象表 ERROR:Error(50,57): PLS-00765: COLUMNS or TABLE is not allowed in this context
- python - 在python中将变量复制到数据框中所有行的快速方法
- ruby - Jekyll 图像破坏 - 不会加载新图像
- linux - 使用 sed 时日期文件之间的转义分隔符
- c# - 在 Xamarin 中使用来自 AndroidKeyStore 的密钥解包封装的 CMS 数据
- vue.js - 如何在 VueJS 模板中声明具有动态名称和动态组件的插槽列表?
- c++ - 将类重新定义为不同类型的符号
- java - log4j 从哪里选择保存日志的驱动器
- c# - 两个 azure web api 如何在 azure 中通信
- java - 为什么局部变量存储在java的堆栈内存中