使用capacitor遇到的问题记录

1. 页面上的axios请求返回没有cookie。通过Charles抓包,在chrome上请求回来能看到cookie,但在capacitor android app上看不到cookie。一开始以为是capacitor的问题,后来搜到在app中hostname默认是localhost,但cookie的domain是web.hjdang.com,导致获取不到cookie。改了下配置,增加hostname:app.hjdang.com,结果发现请求都发不出去。再次改成www.hjdang.com就好了。

{
  "appId": "org.capacitor.hjdang",
  "appName": "好家当",
  "bundledWebRuntime": false,
  "npmClient": "[object Object]",
  "webDir": "www",
  "plugins": {
    "LiveUpdates": {
      "appId": "3d5dd39b",
      "channel": "Production",
      "autoUpdateMethod": "background",
      "maxVersions": 2
    }
  },
  "server": {
    "androidScheme": "https",
    "hostname": "www.hjdang.com"
  }
}

  以上解决方式发现是在capacitor的webview里直接打开了www.hjdang.com这个网址所以没问题。但这却没有用到app里本地localhost的网页。后来经过一阵搜索,发现需要用到CapacitorHttp这个plugin

https://github.com/capacitor-community/http/issues/268

https://www.reddit.com/r/ionic/comments/18um47t/capacitorhttp_vs_axios/

https://forum.ionicframework.com/t/send-authentication-cookies-along-axios-request/233712

 在app里不适合用cookie的方式来auth,而适合awt的方式 /bearer token

Cookies are used to remember something in the stateless environment. An app is not stateless.

https://forum.ionicframework.com/t/sharing-authentication-cookies-from-browser-to-http-api-calls/186249/4

https://forum.ionicframework.com/t/capacitor-ios-cookie-authentication-capacitor-http/237748/4

https://forum.ionicframework.com/t/cookies-vs-preferences-what-is-more-persistent/236820

https://forum.ionicframework.com/t/best-way-to-store-bearer-token-from-api/211483

 

 
 
1
Plugin Icon
 
 
 
1
Plugin Icon
 
 
1
Plugin Icon
posted @ 2025-07-21 23:07  zjhgx  阅读(60)  评论(0)    收藏  举报