For the dataType option to the JQuery.AJAX function, I don't see byte array or blob as one of the possibilities.

对于JQuery.AJAX函数的dataType选项,我没有看到字节数组或blob作为可能性之一。

How can I get it so my server can return a byte array as the result of an AJAX call?

我如何得到它,以便我的服务器可以返回一个字节数组作为AJAX调用的结果?

I could convert the blob to text, but I'm going for compactness.

我可以将blob转换为文本,但我要求紧凑。

EDIT:The blob will not be shown to the user. My javascript will look at it and create an object out of it. It's about a 50kb blob, and speed is important, so I don't want to add any bloat if I don't have to.

编辑:blob不会显示给用户。我的javascript将查看它并从中创建一个对象。它大约是一个50kb的blob,速度很重要,所以如果我不需要,我不想添加任何臃肿。

EDIT:My data is an array of integers. Base64 encoding is a possibility, but I'd prefer not to add bloat. If there isn't a way to do this, I guess I would just Base64 encode it though.

编辑:我的数据是一个整数数组。 Base64编码是可能的,但我不想添加膨胀。如果没有办法做到这一点,我想我只会对它进行Base64编码。

1 个解决方案

#1


3

EDIT: Based on comment discussion, I would like to revise my answer. If you are passing an array of integers, and can get it to that format on your server-side, from there you should absolutely turn it into JSON. JSON supports passing integers, jQuery easily supports getting JSON.

编辑:根据评论讨论,我想修改我的答案。如果你传递一个整数数组,并且可以在服务器端获得它的那种格式,那么你应该把它变成JSON。 JSON支持传递整数,jQuery很容易支持获取JSON。

Sample JSON for an array of ints:

一组int的示例JSON:

{
"array": [0, 1, 2, 3, 4, 5]
}

Sample Javascript/jQuery code for retrieving that JSON:

用于检索JSON的示例Javascript / jQuery代码:

$.getJSON('/url/to/binary/data/returning/json', 
    function(data) {
        // access the array this way:
        var array = data.array;
        var first = array[0];
        // so here you can do whatever your code needs with that array
    }
);

Old Suggestions

老建议

While I agree with the commenters above, I believe that it should be possible to do this. There is a base64 encoder/decoder jQuery plugin that should help in transfering your data. (Causing a bit of bloat, it's true). If you base64 encode your array, you should be able to transfer it.

虽然我同意上述评论者,但我认为应该可以这样做。有一个base64编码器/解码器jQuery插件,可以帮助您传输数据。 (导致一点膨胀,这是真的)。如果你对数组进行base64编码,你应该能够传输它。

If you just want to download binary data (not display it), then set the MIME type of your response to application/octet-stream and give an attachment name for appropriate browser handling.

如果您只想下载二进制数据(不显示它),请将响应的MIME类型设置为application / octet-stream,并为适当的浏览器处理提供附件名称。

$.get('/url/to/binary/data',
    function(data) {
        // convert binary data to whatever format you would like to use here with
        // an encoded string, have the browser download, call
        // a helper function, etc.
    }
);

You may want to consider looking at other formats that are more often transferred via HTTP (or using another transport protocol if necessary), though, depending on what you are trying to do.

您可能需要考虑查看更多通过HTTP传输的其他格式(或者在必要时使用其他传输协议),具体取决于您尝试执行的操作。

更多相关文章

  1. 将php jsonencode数组结果显示为ajax成功函数
  2. 如何使用XMLHttpRequest向服务器发送数组
  3. 【JavaScript】jQuery+ajax传递json数组(局部响应处理)
  4. 创建一个未排序的数组,其中包含重复元素和唯一元素的总和
  5. js不使用jquery,调用ajax,传递数组,并接受,
  6. 使用jQuery.ajax post函数将javascript数组中的数据传递给服务器
  7. 将JavaScript数组转换成逗号分隔列表的简单方法?
  8. 如何将表列转换为数组?
  9. 如何修复JSON对象的假数组?

随机推荐

  1. golang使用什么数据库?
  2. golang怎么自定义错误
  3. golang 如何部署到服务器?
  4. golang解决中文乱码的方法
  5. go语言中的排序讲解
  6. golang怎么给slice赋长度
  7. Go语言异常处理的方法介绍
  8. golang怎么拼接字符串数组
  9. golang使用protobuf的方法详解
  10. golang怎么定时任务