【问题标题】:How to disable request caching or add refresh in ionic 3 app?如何在 ionic 3 应用程序中禁用请求缓存或添加刷新?
【发布时间】:2018-10-22 20:51:18
【问题描述】:

我的 ionic 应用程序有问题: 在 ionViewWillEnter() 上,我通过 GET 请求请求服务器以获取数据。 如果我第一次打开该页面,则会发送请求。 如果是第二次打开,应用程序会读取缓存并且不发送请求。 此问题仅存在于设备上。 任何想法 ? 谢谢。

编辑: 我正在使用这个版本: @ionic/app-scripts:3.2.0
Cordova 平台:ios 4.5.5
离子框架:离子角 3.9.2

所有请求都进入拦截器:

import { Injectable, NgModule} from '@angular/core';

import { Observable } from 'rxjs/Observable';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpHeaders} from '@angular/common/http';
import {Globals} from './globals';
@Injectable()
export class HttpsRequestInterceptor implements HttpInterceptor {
headers : HttpHeaders;
  constructor(private globals: Globals){}
 intercept(
   req: HttpRequest<any>,
   next: HttpHandler): Observable<HttpEvent<any>> {
     if(localStorage.getItem('jwt')){
       this.headers = new HttpHeaders({'Authentication':localStorage.getItem('jwt'),'Cache-Control':['no-cache','no-store'],'Pragma':'no-cache','Expires': '0'});
   }else{
     this.headers = new HttpHeaders({'Authentication':''});
   }
     const dupReq = req.clone({ headers: this.headers });
     return next.handle(dupReq);
   }
};

我的服务器上启用了所有 CORS。

在我的控制器中我这样做:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams,LoadingController } from 'ionic-angular';
import {UserService} from '../../providers/user-service';
import {AlertService} from '../../providers/alert-service';
import { TranslateService } from '../../app/translate';

@IonicPage()
@Component({
  selector: 'page-user-messages',
  templateUrl: 'user-messages.html',
})
export class UserMessagesPage {
messagesList;
  constructor(public navCtrl: NavController, public navParams: NavParams,public translateService:TranslateService,
    public alertService : AlertService, private userService : UserService,public loadingCtrl: LoadingController) {

  }
/**
* Show user's messages
**/
  ionViewWillEnter() {
    let loader = this.loadingCtrl.create({
      content: this.translateService.instant('text','loadingText'),
    });
    loader.present().then(() => {
    this.userService.getMyMessages().subscribe(jsonResponse=>{
      if(jsonResponse.success==true){
        this.messagesList = jsonResponse.rows;
      }else{
        this.alertService.showAlert(this.translateService.instant('error','title'),jsonResponse.msg);
      }
      loader.dismiss();
    },error => {
      this.alertService.showAlert(this.translateService.instant('error','title'),error);
      loader.dismiss();
    });
    });
  }

}

最后,提供者是:

import { Injectable } from '@angular/core';
import { HttpClient,HttpParams } from '@angular/common/http';
import { Globals} from '../app/globals';
import 'rxjs/add/operator/map';
import {Observable} from 'rxjs/Observable';
import {User} from '../app/models/user';
import {JsonResponse} from '../app/models/json-response';
@Injectable()
export class UserService {

  constructor(private httpClient : HttpClient, private globals: Globals){}

  public getMyMessages() : Observable<any>{
    return this.httpClient.get("myurl");
  }

}

【问题讨论】:

标签: angular cordova ionic3 httpclient whitelist


【解决方案1】:

所以,我终于找到了解决方案: 在您的 .htaccess 中,像这样设置缓存控制的标头

Header set Cache-Control "private, s-maxage=0, max-age=0, max-age=0, must-revalidate"

【讨论】:

  • 嘿,你能再解释一下吗?
  • 嗨@wcjord,这是一个老话题:-)由于缓存,我在获取数据时遇到了一些麻烦。我在服务器上的 .htaccess 文件中添加了一个标头,仅此而已。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-06
  • 1970-01-01
  • 2014-05-05
  • 2014-10-05
  • 2016-03-25
  • 1970-01-01
相关资源
最近更新 更多