我使用https://github.com/dcodeIO/ProtoBuf.js/将一条消息编码成了一个名为batch的ByteBuffer,我想将其发送给一个Java服务器。
batch:
ByteBuffer {array: ArrayBuffer, view: DataView, offset: 0, markedOffset: -1, length: 139…}
array: ArrayBuffer
length: 139
littleEndian: false
markedOffset: -1
offset: 0
view: DataView
__proto__: Object
现在我想用jQuery的Ajax发送这个请求:
$.ajax({
url: myUrl,
type: 'POST',
data : batch,
contentType: 'application/protobuf',
accept: 'application/json'
})
但是我似乎做不到这一点。上面的代码给了我jquery异常:
Uncaught TypeError: Cannot set property 'littleEndian' of undefined myScript.js:1353
ByteBuffer.LE bunk.js:1353
jQuery.param.add jquery.js:7203
buildParams jquery.js:7261
jQuery.param jquery.js:7223
jQuery.extend.ajax
如果我从批处理中获取一个数组缓冲区(batch.toArrayBuffer()),那么Java服务器将获取不到任何内容。
ByteStreams.toByteArray(req.getInputStream());
我应该如何编码ByteBuffer以便像这样发送?我应该如何将其解码为Java字节数组?