0

I´m developing a mobile app with PhoneGap and jQuery Mobile. I´m connecting to REST web service in my Web Worker using XMLHttpRequest.

var url = "http://myurl.dyndns.org:9018/WebService/webresources/UsersWS/getUser";
    var params = '{"id":"' + 5 + '"}';
 var xhr;
    try {
        xhr = new XMLHttpRequest();
        xhr.open('POST', url, false);
        xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var result = JSON.parse(xhr.responseText);
                self.postMessage("PRUEBA: "+result.d);
            }
        }; 
        xhr.send(JSON.stringify(params));
    } catch (e) {
        self.postMessage('Error occured in XMLHttpRequest: ' + xhr.statusText + '  ReadyState: ' + xhr.readyState + ' Status:' + xhr.status + ' E: ' +e+' Msg:'+e.message);
    }

In Samsung Galaxy 3 with Android Version 4.3, it works, but in Nexus 7 Tablet with Android Version 4.4.2 is not working. I get this message:

"XMLHttpRequest cannot load http://myurl.dyndns.org:9018/WebService/webresources/UsersWS/getUser. Origin file:// is not allowed by Access-Control-Allow-Origin.", source:  (0)

I have read that Nexus has Chrome Browser and this has some problems (Ref: Origin null is not allowed by Access-Control-Allow-Origin) but I don´t know what can I do, I use xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); but I doesn´t work...

Can you help me, please?

Thanks in advance.

Community
  • 1
  • 1
user1983864
  • 7
  • 1
  • 8

1 Answers1

0

I started to use a very simple webserver to serve the files following this documentation and it worked. The reason is that some webbrowsers(e.g. Safari) handle local files differently than data from a server.

http://4pcbr.com/topic/staticme_simplest_web_server_for_serving_static_files

mojovski
  • 581
  • 7
  • 21