Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery.AJAX - can my server return a blob of data?

Tags:

jquery

ajax

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.

like image 505
Kyle Avatar asked Mar 26 '10 13:03

Kyle


People also ask

Can ajax return data?

You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });

How does big data handle ajax response?

1 Answer. Show activity on this post. Try adding a complete function to see if you are timing out or some other error is happening: If it is a timeout, add a timeout property to the ajax call. Shouldn't he just change the failure function to error , its correct specification?

What type of data does ajax return?

The dataType option specifies the type of response data, in this case it is JSON. The timeout parameter specifies request timeout in milliseconds. We have also specified callback functions for error and success. The ajax() method returns an object of jQuery XMLHttpRequest.


1 Answers

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.

like image 63
justkt Avatar answered Oct 19 '22 00:10

justkt