Js Axios.get 中文編碼問題

本帖最後由 shal 於 2018-6-25 07:53 編輯

我有個project backend 唔系我寫關系
佢個Json and js file 用axios.get 所有中文出方型問号
經過一查之下寫Backend條友用iso8858-1做輪出
死都話唔識用Utf8做

想問下用Axios下有沒方法攪到?

我有個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)

TOP

可以試試

Axios 攞 iso8858-1/raw bytes:
https://github.com/axios/axios/pull/869

之後再由你轉做utf-8:
https://github.com/ashtuchkin/iconv-lite

TOP

本帖最後由 shal 於 2018-6-26 23:00 編輯
可以試試

Axios 攞 iso8858-1/raw bytes:


之後再由你轉做utf-8:
forte 發表於 2018-6-25 12:28



    上星期已試左not work
  1. import Axios from 'axios'
  2. import iconv from 'iconv-lite'

  3. let ResAxios = srcurl.map(surl => Axios.get(surl.toString(), {headers: {
  4.       Accept: '*/*',
  5.       'Cache-Control': 'max-age=0',
  6.       'accept-language': 'en-US,en;q=0.9',
  7.       'Content-Type': 'application/json;charset=UTF-8',
  8.       'Content-Language': 'UTF-8'
  9. }}, {charset: 'latin1'}))
  10. let stockRes = Promise.all(ResAxios).then(response => {
  11.     let results = response.map((item, index) => {
  12.         console.log(item.data)  // out put is ���K���q
  13.         item.data = iconv.decode(item.data, 'ISO-8859-1')
  14.         console.log(item.data)  // out put is "ýýýKýýýq
  15.     })
  16. })
複製代碼

TOP

本帖最後由 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

TOP

arraybuffer return null

TOP

x = new XMLHttpRequest()
x.open('GET',surl.toString())
x.responseType ='arraybuffer'
x.onreadystatechange = () => x.readyState === 4 && console.log(x.response)
x.send()

試下嘔段bytearray出來睇下

TOP

攪掂了
以下方法ok 左
  1. main page head need set
  2. <meta charset="utf-8">

  3. on JS
  4. var response = Axios.get(surl.toString(), {responseType: 'arraybuffer'})
  5.     response.then(data => {
  6.       var textDecoder = new TextDecoder('big5')
  7.       var str = textDecoder.decode(data.data)
  8.       console.log(str)
  9.     })
複製代碼

TOP

本帖最後由 Ferrari2010 於 2018-6-26 23:58 編輯
攪掂了
以下方法ok 左
shal 發表於 2018-6-26 23:00



TextDecoder 仲係experimental, 要注意browser support,
https://caniuse.com/#search=TextDecoder

TOP

如果可以既話, 你叫佢在Content-Type header 後面加返個; charset=big5
eg application/json; charset=big5
就搞掂.
唔應該靠你frontend 做text decode

TOP