Cod*_*ger 9 ios nsurlsession ionic3 cordova-plugin-advanced-http
我想停止缓存我的API请求和响应哪个native-http插件存储其缓存及其创建我的应用程序的问题.
所有时间API工作正常,但当我从服务器收到404或401错误时,它会将其缓存在我的应用程序中,然后在所有时间后我将获得1状态的超时错误.
要解决此问题,我需要卸载应用程序并重新安装它将按预期工作.
任何想法如何停止缓存HTTP请求和响应?
或者如何解决1状态的超时问题?
我在我的请求标题中尝试过以下内容,但仍未成功.
self.httpPlugin.setHeader('*', 'authorization', 'Bearer ' + token);
self.httpPlugin.setHeader('*', 'Cache-control', 'no-cache');
self.httpPlugin.setHeader('*', 'Cache-control', 'no-store');
self.httpPlugin.setHeader('*', 'Expires', '0');
self.httpPlugin.setHeader('*', 'Pragma', 'no-cache');
Run Code Online (Sandbox Code Playgroud)
还在我的请求中添加了虚拟唯一参数,以便像下面那样发出我的API调用的唯一请求.
self.httpPlugin.setHeader('*', 'ExtraDate', new Date().toString());
Run Code Online (Sandbox Code Playgroud)
在Ionic 3中遇到过这种问题的人?
尝试了这个线程的建议,但没有运气.
为此问题建议任何解决方案.
**编辑:**
完整的请求代码:
/**
* Get Search result from server.
*/
getCaseListBySearchText(searchText: string): Observable<any> {
let self = this;
return Observable.create(function(observer) {
self.getToken().then(token => {
console.log("Token : ", token);
// let rand = Math.random();
self.httpPlugin.setHeader("*", "authorization", "Bearer " + token);
self.httpPlugin.setHeader("*", "Cache-control", "no-cache");
self.httpPlugin.setHeader("*", "Cache-control", "no-store");
// self.httpPlugin.setHeader("*", "Expires", "0");
self.httpPlugin.setHeader("*", "Cache-control", "max-age=0");
self.httpPlugin.setHeader("*", "Pragma", "no-cache");
self.httpPlugin.setHeader("*", "ExtraDate", new Date().toString());
self.httpPlugin
.get(self.url + "/CaseList?caseNum=" + searchText, {}, {})
.then(response => {
console.log("Response Success : " + JSON.stringify(response));
let jsonResponse = JSON.parse(response.data);
console.log("JSON OBJECT RESPONSE : " + jsonResponse);
observer.next(jsonResponse);
})
.catch(error => {
if (error.status == 403) {
console.log("Token expired : " + JSON.stringify(error));
self.isTokenExpired = true;
//Removing Old Token
self.storage.remove(Constants.AUTH_DATA);
observer.error(error);
} else {
console.log("Error : " + error);
console.log("Error " + JSON.stringify(error));
observer.error(error);
}
});
})
.catch(error => {
console.log("Error : " + error);
observer.error(error);
});
});
}
Run Code Online (Sandbox Code Playgroud)
经过对上述问题进行大量研发和网络搜索后,我找到了一个解决方案来清理我的请求缓存,因为所有标头都无法清理缓存。
在 HTTP Advanced 插件中,有一种方法可以清除我的 cookie。
clearCookies()
清除所有cookie。
通过在我的类构造函数中使用上述方法,在调用任何 API 之前清除 cookie。
那么它将清除我所有的 cookie 以及与旧 cookie 相关的问题将以这种方式解决。
constructor(
public storage: Storage,
public httpPlugin: HTTP,
private platform: Platform
) {
// enable SSL pinning
httpPlugin.setSSLCertMode("pinned");
//Clear old cookies
httpPlugin.clearCookies();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码解决了我的问题。
感谢大家的快速指导和建议。
如果这不是清除我的旧请求数据的正确方法,请对此发表评论。
归档时间: |
|
查看次数: |
1694 次 |
最近记录: |