Protobuf:WebApi -> JS - 解码对象为空

Protobuf : WebApi -> JS - Decoded object is empty

我想通过 Ajax 请求将对象从 WebApi 控制器发送到 Html 页面。

当我在 JS 中收到对象时,它是空的。但是服务器端对象不是空的,因为当我查看 byte[].length 它大于 0.

最后我自己解决了这个问题。

是客户端出了问题。

  • 事实上 xhr.response 是 JSON 格式,所以它在双引号 "ChEKBFRlc3QQARkfhetRuB4JQA==" 之间。我不得不 JSON.parse 我的回应。enter code here
  • 我删除了 xhr.responseType = "arraybuffer";

这是我的代码:

var ProtoBuf = dcodeIO.ProtoBuf;
var xhr = ProtoBuf.Util.XHR();
xhr.open(
    /* method */ "GET",
    /* file */ "/XXXX/Protobuf/GetProtoData",
    /* async */ true
);
// xhr.responseType = "arraybuffer"; <--- Removed
xhr.onload = function (evt) {
    var testModelBuilder = ProtoBuf.loadProtoFile(
        "URL_TO_PROTO_FILE",
        "Container.proto").build("Container");
    var msg = testModelBuilder.decode64(JSON.parse(xhr.response)); <-- Parse the response in JSON format
    console.log(msg); // Correctly decoded
}
xhr.send(null);