For the dataType option to the JQuery.AJAX function, I don't see byte array or blob as one of the possibilities.
How can I get it so my server can return a byte array as the result of an AJAX call?
I could convert the blob to text, but I'm going for compactness.
EDIT:The blob will not be shown to the use开发者_开发百科r. 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.
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.
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.
Sample JSON for an array of ints:
{
"array": [0, 1, 2, 3, 4, 5]
}
Sample Javascript/jQuery code for retrieving that JSON:
$.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.
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.
$.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.
精彩评论