javascript - Byte array from backend is converted to something else on front-end
问题描述
[Route("encrypted")]
[HttpGet]
public sbyte[] Encrypted()
{
var mm = System.IO.File.ReadAllBytes("C:\\test\\" + "fill.txt");
sbyte[] sbt = new sbyte[mm.Length];
Buffer.BlockCopy(mm, 0, sbt, 0, mm.Length);
return sbt;
}
when I hover over with mouse it shows following bytes (which is correct):
But when I check on the front-end (javascript). It becomes a different arrayBuffer:
Here is the front end code:
var xhr = new XMLHttpRequest();
xhr.open('GET', '/api/encrypted/', true);
xhr.responseType = 'arraybuffer'; //i have tried without this line too
xhr.onload = function (e) {
if (this.status === 200) {
console.log("received from server--------");
console.log(e.currentTarget.response);
console.log("received from server-------");
}
};
xhr.send();
解决方案
您没有提出具体问题,但我认为这可能会有所帮助。
您的控制器操作正在响应 JSON。转储json
到控制台会在前端显示与转储sbt
到后端控制台相同的数组值。这是转储值的前端代码。
var xhr = new XMLHttpRequest();
xhr.open('GET', '/api/values', true);
xhr.responseType = 'json';
xhr.onload = function (e) {
if (this.status === 200) {
console.log("json");
const json = e.currentTarget.response;
console.log(json);
console.log("json");
}
};
因此,您正在发送一个 JSON 数组。
顺便说一句,这里有一些关于arraybuffer
响应类型的链接。
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array
- https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data
推荐阅读
- php - 如何将错误消息与注册表单中的表单字段分开?
- f# - List.unfold/Array.unfold 导致内存爆裂
- c# - Kubernetes集群内的C#gRPC无法将DNS名称解析为其他服务
- javascript - 从 HTML 开发工具中查找 React JS 中的 UI 元素
- php - 以优雅的方式基于关键结果抓取中间的 x 结果
- mysql - Mysql - 选择,存储在var中,在同一个查询中选择
- embedded - 如何使 /var/log 符号链接到 Yocto Rocko 中的持久存储
- r-markdown - 在 RMarkdown 中生成 APA 文本表
- build - 我可以在我的 bazel 目标中提供相对 deps 路径吗?
- python - Django "TemplateView" 和 "/media/" url 冲突