0
votes

XMLHttpRequest - empty responseText but status 200 and readyState 4

I've been trying to do a cross domain request with plain JS. It does work in Firefox and Chromium but not in Android Emulator.

The onload method returns success (status 200 and readyState 4) but the responseText is empty.

window.addEventListener('load', function () {
    var xmlHttpRequest = new XMLHttpRequest();
    xmlHttpRequest.open('GET', 'http://10.2.1.22/'); // My website running in local apache right now. It'll be in a webhosting.

    xmlHttpRequest.addEventListener('load', function () {
        document.body.appendChild(document.createTextNode(this.status + " - " + this.readyState)); // output: "200 - 4"
        document.body.appendChild(document.createTextNode(this.responseText)); // in browser output is a regular string ... in android emulator it's an empty string.
    });

    xmlHttpRequest.addEventListener('error', function (event) {
        document.body.appendChild(document.createTextNode("Error!")); // it's never called.
    });

    xmlHttpRequest.send();
});

My website sends these headers:

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=UTF-8');

Cordova version:

$ cordova --version
3.4.0-0.1.3

EDIT

app_root/config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.xmlhttprequest" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>XMLHttpRequest</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="[email protected]" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <access origin="*" />
</widget>
1

1 Answers

0
votes

Did you add your site to the whitelist in config.xml? By default, all documents that are whitelisted will return an empty document if you didn't explicitly whitelist it. This is by design. You should probably read about the whitelist:

http://docs.phonegap.com/en/3.4.0/guide_appdev_whitelist_index.md.html#Whitelist%20Guide