0

Hi have created a XMLHttpRequest and get the resp by using the following code.

    var xhr = new XMLHttpRequest();
    xhr.open("GET", URL, true);
    xhr.responseType = "text";
    xhr.onload = function () 
    { 
        debugger;

        var resp = xhr.response;

        var result = msgpack.unpack(resp);

    };

but the response in undefined. I have checked the service from iOS and it is working fine.

Arahim
  • 303
  • 1
  • 6
  • 14
  • Are you sure that request was complete and successful when you call `xhr.response`? Have you tried using `onreadystatechange` instead of `onload`? – Ilya Luzyanin Aug 29 '14 at 14:39

1 Answers1

0

Fixed by the following code. Set response type = arraybuffer.

    var xhr = new XMLHttpRequest();
    xhr.open("GET", URL, true);
    xhr.responseType = "arraybuffer";
    xhr.onload = function () 
    { 
        var resp = xhr.response;

        var uintDataArray = new Uint8Array(resp);
        var strBytes = "";

        for (var i = 0; i < uintDataArray.length; i++)  
        {
            strBytes += String.fromCharCode(uintDataArray[i]);
        }

        var result = msgpack.unpack(strBytes);

    };
Arahim
  • 303
  • 1
  • 6
  • 14