tags:

views:

457

answers:

2

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 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.

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.

A: 

Could you explain a little bit more why you want to do this?

Jamie
I know that you are a short way of being able to comment, but you we have all been there. Try to wait a bit longer rather then using the answer to ask a comment.
thecoshman
Ok. Sorry :-) I did wonder why I couldn't comment...
Jamie
If you fancied cleaning up the place, you can even delete this answer.
thecoshman
+1  A: 

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.

justkt
Could you give an example of setting the MIME type to 'application.octet-stream'.
thecoshman
So, if I set the MIME type to application/octet-stream, then the `data` variable in your code snippet would contain it? Would the `data` variable be of type byte array?
Kyle
@thecoshman - It's been years since I used PHP, so I'm not sure exactly how to set up HTTP headers from the server in PHP. Looks like a Google search provides pointers to the PHP manual to explain.
justkt
@Spines - if you set the MIME type to application/octet-stream, the browser will LIKELY try to download it - as in pop up the save dialog - although it depends on user settings. To pass data as text to get it to end up in the jQuery success callback, convert your array to a string, base64 encode it, then pass it as text/plain. On the client side, base64 decode it and then you can use the proceedure described in another StackOverflow answer to get to it: http://stackoverflow.com/questions/1240408/reading-bytes-from-javascript-string.
justkt
Your code that wants to work on data should be in that function. A byte array makes sense to me as to the format.
thecoshman
Thanks justkt, that format could still be quite a bit of bloat though. 3 ints which take 12 bytes could turn into `[3262234,55375,765634]` which is 20 bytes not including the brackets. Though if the ints were mainly small numbers I guess it wouldn't add much.
Kyle