js请求gbk接口的问题

前端是vue,后端是java,现在后端接口是gbk的,前端如何改成能够正常调用接口?尤其是请求参数如何转gbk?


网上找到了这段


1、对传入的GBK字符串,要用数据流接收,具体到angularjs中,$http 请求中需要覆盖参数responseType , responseType: "arraybuffer",



$http({

  method: "POST",

  responseType: "arraybuffer",

  url: "restcater/cenchain/findCenChain",

  data: branchlist

})

2、解析

var x = new Uint8Array(resp.data);


var str =new TextDecoder('gbk').decode(x);


已经正常识别了。

/////////////////////////////////////////////////////////

3、UTF-8提交的数据转为GBK,要引用第三方JS库


https://github.com/inexorabletash/text-encoding


<script>

  // var TextEncoderOrg = window.TextEncoder;

  // ... and deactivate it, to make sure only the polyfill encoder script that follows will be used

  window.TextEncoder = null;

</script>

<script src="lib/text-encoding/encoding-indexes.js"></script>

<script src="lib/text-encoding/encoding.js"></script>

  //获取GBk编码的int8数组 

var uint8array =  new TextEncoder("gbk",{ NONSTANDARD_allowLegacyEncoding: true }).encode(string);

 // 放入blob中准备上传

 var blob=new Blob([uint8array],{type:"text/plain"});

但实际使用UTF-8提交的数据转为GBK这个似乎没有用啊,转出来得到的uint8array 是个数组,调用接口参数还需要进行des加密,需要变成字符串,这又要怎么做呢?


慕容3067478
浏览 919回答 1
1回答

Helenr

AJAX 请求,参数值的编码要使用&nbsp;encodeURIComponent&nbsp;,它只支持 UTF-8 的字节结果,无法产生 GBK 的字节结果。form&nbsp;提交,可以使用&nbsp;accept-charset&nbsp;属性指定编码的字节结果。(form&nbsp;的话自己好像就会根据页面的 charset 来默认编码了)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript