I would like to send an object from a WebApi
controller to an Html page through an Ajax
Request.
When I receive the object in JS, it's empty. But server-side the object isn't empty because when I look at the byte[].length
it's greater than 0.
- Server-side, I use the dll provided by Google.
JS side, I use the ProtobufJS library. This is my
.proto
file :syntax="proto3"; message Container { repeated TestModel2 Models = 1; } message TestModel2 { string Property1 = 1; bool Property2 = 2; double Property3 = 3; }
Server code :
var container = new Container(); var model = new TestModel2 { Property1 = "Test", Property2 = true, Property3 = 3.14 };
container.Models.Add(model);
Base64 data :
ChEKBFRlc3QQARkfhetRuB4JQA==
JS decoding :
var ProtoBuf = dcodeIO.ProtoBuf; var xhr = ProtoBuf.Util.XHR(); xhr.open( /* method */ "GET", /* file */ "/XXXX/Protobuf/GetProtoData", /* async */ true ); xhr.responseType = "arraybuffer"; xhr.onload = function (evt) { var testModelBuilder = ProtoBuf.loadProtoFile( "URL_TO_PROTO_FILE", "Container.proto").build("Container"); var msg = testModelBuilder.decode64(xhr.response); console.log(JSON.stringify(msg, null, 4)); // Correctly decoded } xhr.send(null);
Result object in JS console :
{ "Models": [] }
bytebuffer.js
- protobuf.js v5.0.1
console.log
the response. If its not empty there then we can go through the decoding process. We need to see error's because we can't debug for you. & I assumed that you're not authenticating headers on requests with keys so if so, you should be able to directly request it through the URL as see the absolute response your AJAX request would be getting - then work from there. That's assuming this API was built by you also. – Motivateconsole.log
the response I obtain thisArrayBuffer {}
So I suppose that the response is empty. But server-side, when I serialize my object inbyte[]
, the length is > 0 and I am able to deserialize it and its not empty. Plus, in the Chrome console the ajax response isChEKBFRlc3QQARkfhetRuB4JQA==
– Wafd