You are here: Reference > JavaScript > client-side > xml handling > properties > responseBody (XMLHttpRequest)

responseBody property (XMLHttpRequest)

Browser support:
Returns the body of the server's response as an array of unsigned bytes.
This property can be useful when the response body is binary or when it is text, but the responseText property uses wrong character encoding.

Syntax:

object.responseBody;
You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

Returns the response content as a byte stream.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the responseBody property with ADODB.Stream object to convert a shift_jis (Japanese) string returned from the server.
The default security settings of Internet Explorer do not allow using this ActiveX object. To try this example, enable the 'Initialize and script ActiveX controls not marked as safe for scripting' option placed in Tools/Internet Options/Security/Custom level.../ActiveX controls and plug-ins. After testing this example, set this option to back to its default state, because the enabled state is unsafe!
Code
ajax.js
jp.txt
<head>
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="text/javascript">
        var httpRequest = null;

        function SendRequest () {
            if (!httpRequest) {
                httpRequest = CreateHTTPRequestObject ();   // defined in ajax.js
            }
            if (httpRequest) {          
                    // The requested file must be in the same domain that the page is served from.
                var url = "jp.txt";
                httpRequest.open ("GET", url, true);    // async
                httpRequest.onreadystatechange = OnStateChange;
                httpRequest.send (null);
            }
        }

        function OnStateChange () {
            if (httpRequest.readyState == 0 || httpRequest.readyState == 4) {
                if (IsRequestSuccessful (httpRequest)) {    // defined in ajax.js
                    var output = document.getElementById ("output");
                    var outputRepaired = document.getElementById ("outputRepaired");

                    output.innerHTML = httpRequest.responseText;

                    if (typeof (ActiveXObject) != "undefined" && typeof (httpRequest.responseBody) != "undefined") {
                        // Convert httpRequest.responseBody byte stream to shift_jis encoded string
                        var stream = new ActiveXObject("ADODB.Stream");
                        stream.Type = 1; // adTypeBinary
                        stream.Open ();
                        stream.Write (httpRequest.responseBody);
                        stream.Position = 0;
                        stream.Type = 2; // adTypeText;
                        stream.CharSet = "shift_jis";
                        var repairedText = stream.ReadText (stream.Size);

                        outputRepaired.innerHTML = repairedText;
                    }
                    else {
                        alert ("Your browser doesn't support this functionality.");
                    }
                }
                else {
                    alert ("Operation failed.");
                }
            }
        }

    </script>
</head>
<body onload="SendRequest ()">
    Content of the responseText property: <br />
    <div id="output" style="width:460px; height:50px; overflow:auto; border:1px solid #000000; background-color:#f5e4b1;"></div>
    <br />
    Content of the responseBody property convert to shift_jis encoded string: <br />
    <div id="outputRepaired" style="width:460px; height:50px; overflow:auto; border:1px solid #000000; background-color:#f5e4b1;"></div>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content