首页 > 解决方案 > 带有 UTF-8 的 axios setRequestHeader 方法

问题描述

我正在尝试在我的请求中添加一些标头。但是标头值可能包含一些土耳其语字符(如 ğ、ş、ö 等),当我尝试这样做时,我得到了这个错误:

TypeError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': String contains non ISO-8859-1 code point.
    at setRequestHeader (xhr.js:132)
    at Object.forEach (utils.js:253)
    at dispatchXhrRequest (xhr.js:126)
    at new Promise (<anonymous>)
    at xhrAdapter (xhr.js:13)
    at dispatchRequest (dispatchRequest.js:52)
    at :3000/report/async http:/localhost:3000/static/js/main.chunk.js:4137

我猜 axios 在将我的自定义标题添加到标题列表时支持 ISO-8859-1 字符。是否可以将 axios 支持的字符集更改为 UTF-8?

标签: node.jsreactjsutf-8character-encodinghttprequest

解决方案


让 headers = {"userId": user.id, "username":user.username, "relatedName": encodeURIComponent(values.username) }; 在 React 上编码。relatedName = URLDecoder.decode(request.getHeader("relatedName"), StandardCharsets.UTF_8.name()); 在 Java 中解码。


推荐阅读