msgpack 的 Sinatra 二进制返回 -- 字符集问题/字符在某处转换?
我目前正在尝试从 ruby sinatra 服务返回 msgpack http://msgpack.org/ 并使用 javascript 解析它。我正在使用在这里找到的javascript库: https://github.com/uupaa/msgpack.js/< /a> (尽管我认为这与这个问题无关)。
我有一个 sinatra 服务,它使用 msgpack gem 执行以下操作:
require 'sinatra'
require 'msgpack'
get '/t' do
content_type 'application/x-msgpack'
{ :status => 'success', :data => {:one => "two", :three => "four"}}.to_msgpack
end
我有 javascript,其读取如下:
<script src="js/jquery.js"></script>
<script src="js/msgpack.js"></script>
<script type="text/javascript">
function r() {
$.ajaxSetup({
converters: {
"text msgpack": function( packed ) {
if(packed != '') {
unpacked = msgpack.unpack(packed);
return unpacked;
}else{
return ''
}
}
}
});
$.ajax({
type: "GET",
url: "/t",
dataType: "msgpack",
success: function(data) {
alert(data)
}
})
}
$(document).ready(r)
</script>
问题是,当我取回数据时,许多字符已从其服务器端版本转换为 0xfffd< /代码>。
然后我尝试了两种变体:
content_type 'application/octet-stream'
和
content_type 'application/octet_stream', :charset => 'binary'
在服务器端。前者没有改变任何东西,但后者更接近,除了一个例外之外,大部分消息保持不变:第一个字符从 0x82
转换为 0x201a
。
我怀疑有一个字符集/内容类型的组合可以解决这个问题,但我还没有尝试过。我也可以随时退回到 Base64,但我想首先了解如何在没有 Base64 的情况下使其正常工作。
I'm currently trying to return msgpack http://msgpack.org/ from a ruby sinatra service and parse it using javascript. I am using the javascript library found here: https://github.com/uupaa/msgpack.js/ (though I don't think that's relevant to this question).
I have a sinatra service that does the following using the msgpack gem:
require 'sinatra'
require 'msgpack'
get '/t' do
content_type 'application/x-msgpack'
{ :status => 'success', :data => {:one => "two", :three => "four"}}.to_msgpack
end
I have javascript that reads it as follows:
<script src="js/jquery.js"></script>
<script src="js/msgpack.js"></script>
<script type="text/javascript">
function r() {
$.ajaxSetup({
converters: {
"text msgpack": function( packed ) {
if(packed != '') {
unpacked = msgpack.unpack(packed);
return unpacked;
}else{
return ''
}
}
}
});
$.ajax({
type: "GET",
url: "/t",
dataType: "msgpack",
success: function(data) {
alert(data)
}
})
}
$(document).ready(r)
</script>
The problem is that when I get the data back, many characters have been converted from their server side version to 0xfffd
.
I then tried the two variants:
content_type 'application/octet-stream'
and
content_type 'application/octet_stream', :charset => 'binary'
on the server side. The former didn't change anything but the latter came closer, leaving most of the message untouched with one exception: the first character was converted from 0x82
to 0x201a
.
I suspect that there is a combination of charset/ content types that would fix this that I haven't tried yet. I could also always fall back to Base64, but I'd like to understand what it takes to get it working without Base64 first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
0x82
是 Latin1 中的低引号
,0x201a
是 UTF-16 中的相同字符。查看您的库如何处理编码,告诉它们使用二进制编码,而不是尝试编码之间的任何转换。UTF-16 有 JavaScript 的味道。如果您使用 jQuery,请查看 http://blog.vjeux。 com/2011/javascript/jquery-binary-ajax.html。
0x82
isLOW QUOTATION MARK
in Latin1,0x201a
is the same character in UTF-16. Have a look at how your libraries deal with encoding, tell them to use a binary encoding and not try any conversion between encodings.UTF-16 smells of JavaScript. If you use jQuery, have a look at http://blog.vjeux.com/2011/javascript/jquery-binary-ajax.html.