ionic之angular攔截器

來源:互聯網
上載者:User

標籤:

ionic作為應用,肯定和伺服器有資料交換,分散處理api太繁瑣,所以一般用攔截器來集中處理。

主要由以下幾個方面的應用:

  1. 伺服器位址注入
  2. 錯誤處理
  3. token注入
  4. Tlog
  5. 無token時跳轉至登入頁面
  6. ...

有四個方法

request: function(config)requestError: function(config)response: function(response)responseError: function(response)

基本看方法名稱就知道其意義。具體的用法、寫法請參考:http://my.oschina.net/ilivebox/blog/290881?p=1

1、伺服器注入、token注入:
request: function(config) {   if (config.params)       config.params[‘x-session-token‘] = CacheService.token();   // 遠程服務,統一添加伺服器位址   var uri = config.url;   if (config.method == "JSONP") {      config.url = fGetHost() + uri;      config.uri = uri;   }   return config;}
2、token逾時:
response: function(response) {    if (response.config.method == "JSONP") {        if (!fIsWhiteUri(response.config.uri)) {            var login = response.data[‘x-session-login‘];            // 強制使用者登入            if (!login) {               $location.path(‘/user/login‘);               return $q.reject(response);            }        }    }    return response;}
說明:1、每次都傳入 x-session-token參數以便後端驗證;

2、後台返回

x-session-login:false
表示token到期,需要重新登入。





ionic之angular攔截器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.