作者: shal 時間: 2018-6-25 07:49 標題: Js Axios.get 中文編碼問題
本帖最後由 shal 於 2018-6-25 07:53 編輯
我有個project backend 唔系我寫關系
佢個Json and js file 用axios.get 所有中文出方型問号
經過一查之下寫Backend條友用iso8858-1做輪出
死都話唔識用Utf8做
想問下用Axios下有沒方法攪到?
作者: hihihi123hk 時間: 2018-6-25 08:21
我有個project backend 唔系我寫關系
佢個Json and js file 用axios.get 所有中文出方型問号
經過一查之下 ...
shal 發表於 2018-6-25 07:49
覺得似係 DB 唔係用 UTF-8 儲多啲
via HKEPC IR Pro 3.4.0 - iOS(2.3.3)
作者: forte 時間: 2018-6-25 12:28
可以試試
Axios 攞 iso8858-1/raw bytes:
https://github.com/axios/axios/pull/869
之後再由你轉做utf-8:
https://github.com/ashtuchkin/iconv-lite
作者: shal 時間: 2018-6-25 13:42
本帖最後由 shal 於 2018-6-26 23:00 編輯
上星期已試左not work
- import Axios from 'axios'
- import iconv from 'iconv-lite'
- let ResAxios = srcurl.map(surl => Axios.get(surl.toString(), {headers: {
- Accept: '*/*',
- 'Cache-Control': 'max-age=0',
- 'accept-language': 'en-US,en;q=0.9',
- 'Content-Type': 'application/json;charset=UTF-8',
- 'Content-Language': 'UTF-8'
- }}, {charset: 'latin1'}))
- let stockRes = Promise.all(ResAxios).then(response => {
- let results = response.map((item, index) => {
- console.log(item.data) // out put is ���K���q
- item.data = iconv.decode(item.data, 'ISO-8859-1')
- console.log(item.data) // out put is "ýýýKýýýq
- })
- })
作者: forte 時間: 2018-6-25 15:16
本帖最後由 forte 於 2018-6-25 15:21 編輯
上星期已試左not work
import Axios from 'axios'
import iconv from 'iconv-lite'
let ResAxios ...
shal 發表於 2018-6-25 13:42
Why is {charset: 'latin1'} the third parameter of axios.get (according to doc, axios.get(url[, config])?
OR you can try setting responseType: 'arraybuffer' in axios then use iconv-lite
https://github.com/axios/axios/issues/332
EDIT:
looks like the later version of axios now uses 'responseEncoding' instead of 'charset', not sure which version you are using
作者: shal 時間: 2018-6-26 08:09
arraybuffer return null
作者: Ferrari2010 時間: 2018-6-26 21:58
x = new XMLHttpRequest()
x.open('GET',surl.toString())
x.responseType ='arraybuffer'
x.onreadystatechange = () => x.readyState === 4 && console.log(x.response)
x.send()
試下嘔段bytearray出來睇下
作者: shal 時間: 2018-6-26 23:00
攪掂了
以下方法ok 左
- main page head need set
- <meta charset="utf-8">
- on JS
- var response = Axios.get(surl.toString(), {responseType: 'arraybuffer'})
- response.then(data => {
- var textDecoder = new TextDecoder('big5')
- var str = textDecoder.decode(data.data)
- console.log(str)
- })
作者: Ferrari2010 時間: 2018-6-26 23:25
本帖最後由 Ferrari2010 於 2018-6-26 23:58 編輯
TextDecoder 仲係experimental, 要注意browser support,
https://caniuse.com/#search=TextDecoder
作者: Ferrari2010 時間: 2018-6-26 23:59
如果可以既話, 你叫佢在Content-Type header 後面加返個; charset=big5
eg application/json; charset=big5
就搞掂.
唔應該靠你frontend 做text decode
作者: shal 時間: 2018-6-27 09:42
如果可以既話, 你叫佢在Content-Type header 後面加返個; charset=big5
eg application/json; charset=big5 ...
Ferrari2010 發表於 2018-6-26 23:59
因為後端間公司唔服 個客搵個做前端 同 multiOS apps (electron)
作者: shal 時間: 2018-6-27 09:45
呢個唔系問題 我會前面做左detec個user browser 先
因為全html5 web app 所以點都會做detec 左先

