Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用nodeFetch正确显示JSON API响应中的特殊字符_Javascript_Html_Json_Reactjs_Special Characters - Fatal编程技术网

Javascript 使用nodeFetch正确显示JSON API响应中的特殊字符

Javascript 使用nodeFetch正确显示JSON API响应中的特殊字符,javascript,html,json,reactjs,special-characters,Javascript,Html,Json,Reactjs,Special Characters,我有一个react应用程序,它通过HTTP与RESTful API通信。 API返回可能包含特殊字符的JSON响应体 示例响应可能如下所示: { "name": "Property One", "description": "Res Thick Film 0805 49.9 Ohm 1% 0.25W(1/4W) ±100ppm/°C Pad SMD Automotive T/R", ... } "Res Thick Film 0805 49.9 Ohm 1% 0.25W

我有一个react应用程序,它通过HTTP与RESTful API通信。 API返回可能包含特殊字符的JSON响应体

示例响应可能如下所示:

{
    "name": "Property One",
    "description": "Res Thick Film 0805 49.9 Ohm 1% 0.25W(1/4W) ±100ppm/°C Pad SMD Automotive T/R",
    ...
}
"Res Thick Film 0805 49.9 Ohm 1% 0.25W(1/4W) �100ppm/�C Pad SMD Automotive T/R"
特殊字符为(但不限于)±和°

我使用
nodeFetch
执行请求,如下所示:

nodeFetch(url, { method: 'GET', headers }).then(response => response.json());
如果我在使用
response.json()
对对象进行反序列化后观察该对象,则
description
属性现在如下所示:

{
    "name": "Property One",
    "description": "Res Thick Film 0805 49.9 Ohm 1% 0.25W(1/4W) ±100ppm/°C Pad SMD Automotive T/R",
    ...
}
"Res Thick Film 0805 49.9 Ohm 1% 0.25W(1/4W) �100ppm/�C Pad SMD Automotive T/R"

如何阻止特殊字符被替换为�?

您也可以看看这个问题,cskau-g repost可能会对您有所帮助


我认为问题在于编码为utf-8,而内容类型必须设置为text/html

您可能应该将标题设置为:
content-type
text/html;字符集=utf8
nodeFetch
支持
UTF-8
开箱即用

就像
nodeFetch
自动处理UTF-8一样。您可以控制REST服务器吗?您能将其设置为utf-8编码其响应吗<代码>'内容类型:text/html;charset=utf-8'@DSCH我确实有访问权。将
内容类型
更改为
应用程序/json;charset=UTF-8和nodeFetch正确地反序列化了响应!回答这个问题,这样我就可以奖励赏金了!您使用的是哪个版本的nodefetch?如果
nodefetch
的版本是
2.X
,则根本不需要在服务器端进行更改。但不确定OP使用的是什么版本