【发布时间】:2020-06-30 13:59:24
【问题描述】:
首先很抱歉,如果以前有人问过这个问题,我尝试了其他类似的问题,但答案对我没有帮助。
我正在使用 fetch() 获取 UTF-8 编码文件,但我的 response.text() 不断返回 undefined。我在 Postman 中测试过,它返回的编码数据没有问题。
示例代码:
async function testCode() {
const response = await fetch(url1);
const data = await response.text();
console.log(response);
console.log(data);
return data;
}
Console.log(数据):
{"_bodyBlob": {"_data": {"__collector": [Object], "blobId": "219BA1C8-5BF8-41E3-97C9-BA0A7684D712", "name": "mgaonline.ubx", "offset": 0, "size": 9416, "type": "application/ubx"}}, "_bodyInit": {"_data": {"__collector": [Object], "blobId": "219BA1C8-5BF8-41E3-97C9-BA0A7684D712", "name": "mgaonline.ubx", "offset": 0, "size": 9416, "type": "application/ubx"}}, "bodyUsed": true, "headers": {"map": {"cache-control": "private", "content-disposition": "attachment; filename=mgaonline.ubx", "content-length": "9416", "content-type": "application/ubx", "date": "Tue, 30 Jun 2020 14:17:13 GMT", "server": "Microsoft-IIS/10.0", "x-aspnet-version": "4.0.30319", "x-powered-by": "ASP.NET"}}, "ok": true, "status": 200, "statusText": undefined, "type": "default", "url": "https://online-live1.services.u-blox.com/GetOnlineData.ashx?token=Janf2Rk0CkKHlMi2hWUOjg;gnss=gps,glo,gal,qzss;datatype=eph,alm,aux;"}
当我尝试访问 [Object] 时,它会显示为 undefined
但在 Postman 中,它返回正确的 utf-8 编码的 8 位二进制数据。
【问题讨论】:
-
@HarmandeepSinghKalsi 这不是 json,这是他们自己的 ubx 类型,但我会尝试使用它
-
你能显示“console.log(data)”吗?愚蠢的问题,但你确定这是你需要的 GET 请求吗?
-
@Arcord 将在问题中作为编辑发布,因为它与评论相比太长了
-
当然 :-) 当你尝试“.blob()”或“.arrayBuffer()”时,你得到了什么吗?如果是,我猜这与您在响应中获得的接受标头的内容有关...由于浏览器不知道它(不认为这是标准的),也许他拒绝将内容作为文本提供给您?
标签: javascript react-native fetch