0

I'm trying to get some info from my XBMC server via javascript. Eventually my goal is to modify/expand this script and use it in tasker, but for now i'm just trying to get it to work in the browser. It works fine in IE and writes '{' to the screen as it should, but when I try to run it in chrome, req.onload never seems to be called. The same is true when i run a modified version on android/tasker. Any Ideas?

function main(){
    var url = "http://192.168.1.85";
    var command = '/jsonrpc?request={"jsonrpc": "2.0", "method": "AudioLibrary.GetArtists", "params": { "sort": { "order": "ascending", "method": "artist", "ignorearticle": true } }, "id": 1}';
    var http_timeout = 1200;

    var req = new XMLHttpRequest();
    req.open('GET', url+encodeURI(command), true);
    req.timeout = http_timeout;

    req.onload = function(e) {
        if (req.readyState == 4 && req.status == 200) {
            if(req.status == 200) {
                document.write(req.responseText);
            }
        }
    };
    req.send(null);
}

TL;DR
This works in IE and not Chrome. Why?

rthill
  • 143
  • 1
  • 7
  • 2
    every browser tends to do the ajax stuff differently. Just use jquery and let it worry about the cross-browser functionality for you. jquery would reduce the entirety of your function to a single line. `$.get('http://......', ...);`. – Marc B Feb 23 '14 at 03:07
  • What is the error message in the console saying? It could also be because of the local network address. –  Feb 23 '14 at 03:25
  • @MarcB I'm pretty new to anything more than the most basic javascript and have never used jquery, could you elaborate please? – rthill Feb 23 '14 at 03:48
  • https://api.jquery.com/jQuery.get/ – Marc B Feb 23 '14 at 03:48
  • @jeff Chrome says: XMLHttpRequest cannot load http://192.168.1.85/jsonrpc?request=%7B%22jsonrpc%22:%20%222.0%22,%20%22met…22artist%22,%20%22ignorearticle%22:%20true%20%7D%20%7D,%20%22id%22:%201%7D. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. – rthill Feb 23 '14 at 03:49
  • Also, if you do decide to use jQuery, you would have to have a script tag before the one containing this code containing a link to the jQuery JS library before you could use that method. – Claudia Feb 23 '14 at 06:35
  • For Cross Origin see the article: http://stackoverflow.com/questions/10143093/origin-is-not-allowed-by-access-control-allow-origin. –  Feb 23 '14 at 13:36

0 Answers0