0

I am trying to call a Hessian web service from a Javascript application, but I'm having issues parsing the response, since jQuery is treating the response as text and stripping the first bytes of it. In my research, I have found out that you need to set the charset as 'charset=x-user-defined' in order to the browser leave my bytes as is. But, according the ajax docs:

Sending Data to the Server

By default, Ajax requests are sent using the GET HTTP method. If the POST method is required, the method can be specified by setting a value for the type option. This option affects how the contents of the data option are sent to the server. POST data will always be transmitted to the server using UTF-8 charset, per the W3C XMLHTTPRequest standard.

And indeed, the charset is not changing regardless of the settings I used. I have tried the following, separately and all at once, with no luck

$.ajax({
        type : 'POST',
        url : url,
        timeout : 3000,
        data : parameters,

        contentType : "x-application/hessian; charset=x-user-defined'",
        mimeType: 'text/plain; charset=x-user-defined',
        headers: { 
            Accept : "text/plain; charset=x-user-defined",
            "Content-Type": "text/plain; charset=x-user-defined"
        },
        beforeSend : function(xhr) {                
            xhr.overrideMimeType("text/plain; charset=x-user-defined");            
        }
    })

Also I tried to mess around with the data converters and custom contenttypes defined in jQuery, with no succes.

It appears that as per the standard, I will not be able to do this. It works with GET but not with POST, and the Hessian protocol requires POST.

Do you have any ideas? Or do I need to start to build my XHR method form scratch?

FreemanAMG
  • 91
  • 2
  • 9
  • Is your question about the posted data, or the reply data? You wrote _jQuery is treating the reply as text_, but then quoted a section of the documentation about the posted data. – Barmar May 22 '13 at 23:02
  • It's true! I haven't notice that it says "to the server". My issue is reading data FROM the server. And, I believe, since jQuery is not willing to change the header, I'm not going to get the binary response I need. – FreemanAMG May 22 '13 at 23:12
  • The way the response is processed is controlled by the `dataType` option. Unless you specify `json`, `xml`, or `jsonp`, or the server sets one of these in the `Content-type` response header, I think it just returns the data verbatim as a string. – Barmar May 22 '13 at 23:15
  • No luck. I just, for instance, tried 'dataType:"html"', because that way, jQuery is not supposed to parse the data. But the response is still treated as string. The first bytes (in HEX) of my response, as I see them in Fiddler are 37 63 31 0D 0A 48 02 00 52 43 1B 68 65 73 73 69 61 6E 2E 64 6F 6D 61 69 6E 61 6E 2E 64 6F 6D 61 69 6E But when I try to parse them, The response is trimmed 02 00 52 43 1B 68 65 73 73 69 61 6E 2E 64 6F 6D 61 69 6E 61 6E 2E 64 6F 6D 61 69 6E – FreemanAMG May 22 '13 at 23:19

1 Answers1

1

Turns out that I was making a silly mistake somewhere else. But anyhow, I found a sweet way for handling binary data on request and responses, from here.

define(function() {
// Do setup work here
function configurationException(message) {
    throw new Error(message + " missing from configuration object");
}

return {
    post : function(config) {
        if (config) {
            var url = config.url || configurationException("url");
            var done = config.done || configurationException("callback function");
            var timeout = config.timeout || 10000;
            var data;
            if (config.data) {
                data = config.data;
            } else {
                data = null;
                console.warn('No data is specified in binaryPost');
            }

            var request = new XMLHttpRequest();
            request.open("POST", url, true);
            request.responseType = "arraybuffer";
            request.setRequestHeader("Content-Type", "x-application/hessian;");

            request.onload = function(oEvent) {
                var arrayBuffer = request.response; // Note: not oReq.responseText
                if (arrayBuffer) {
                    var byteArray = new Uint8Array(arrayBuffer);

                    done(byteArray);
                }
            };

            request.send(data);

        } else {
            throw new Error("Configuration object is missing");
        }
    }
};
});

Hope you find it useful

FreemanAMG
  • 91
  • 2
  • 9