JSON and javascipt
#1
Please help....

I have read 228 pages of the JSON development thread twice over as well as covered other threads with searches.
I am yet to find a solution to my issue.

All I'm after is a simple way to POST JSON commands via Javascript.

I have tried creating functions, tried JQuery and AJAX,
I have seen and written that much code in the last 2 weeks that I feel I'm loosing brain cells at an extremely high rate.

In the old HTTP API i did it this way:
Code:
function xbmc(command){
        
            var xmlhttp = new XMLHttpRequest();
            var url = "http://";
            var end = "/xbmcCmds/xbmcHttp";
            var params = "command=ExecBuiltIn&parameter=";
            xmlhttp.open("GET",url + XBMCnetwork.XBMCip +":"+ XBMCnetwork.XBMCport + end +"?"+ params + command, true);
            xmlhttp.send(null);
}

Using the BuiltIn commands.

I am limited to Javascript ( no python ) it is coded for a Home Automation box (Please don't look away!)
It says it supports jQuery and I've treid to utilize code from AWX's git with no luck.

I can map most direct commands via TCP, but what I'm ideally after is a bit of Javascript that pulls info of currently playing item.
If I can just get the code to work with a simple command, I would be able to manipulate it to send the correct command to get the info.

Here is a sample of code I tried:
Code:
function xbmc(command){
        
            var xmlhttp = new XMLHttpRequest();
            var ip = XBMCip;
            var port = XBMCport;
            var url = "http://"+ ip +":"+ port +"/jsonrpc";
            var params = '{"jsonrpc": "2.0", "method": "Input.Left",  "id": "1"}';
            xmlhttp.open("POST", url, false);
            xmlhttp.setRequestHeader("Content-type", "application/json");
            
            
            xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
                if(http.readyState == 4 && http.status == 200) {
                    alert(http.responseText);
                }
                
            }
            xmlhttp.send('{"jsonrpc": "2.0", "method": "Input.Left",  "id": "1"}');
    }
}

Hope you can help....


Reply
#2
Using jQuery
Code:
$.ajax({
  type: 'POST',
  url: '/jsonrpc?awxi',
  data: '{"jsonrpc": "2.0", "method": "Player.GetItem", "params": { "properties": ["title", "album", "artist", "duration", "thumbnail", "file", "fanart", "streamdetails"], "playerid": 0 }, "id": 1}', //this is audio player
  success: function(result, textStatus, XMLHttpRequest) { console.log(result) },
  dataType: 'json',
  contentType: 'application/json'
});
Image
AWXi - Ajax web interface. Wiki
Reply
#3
I don't see anything obviously wrong (but it has been 5-6 years since I last wrote javascript myself) apart from defining the json-rpc request in the "params" variable and then not using it in xmlhttp.send(). Do you get any error? Can you post the debug log of XBMC because it will show any incoming JSON-RPC request.
Always read the online manual (wiki), FAQ (wiki) and search the forum before posting.
Do not e-mail Team Kodi members directly asking for support. Read/follow the forum rules (wiki).
Please read the pages on troubleshooting (wiki) and bug reporting (wiki) before reporting issues.
Reply
#4
(2012-09-11, 22:46)Mizaki Wrote: Using jQuery
Code:
$.ajax({
  type: 'POST',
  url: '/jsonrpc?awxi',
  data: '{"jsonrpc": "2.0", "method": "Player.GetItem", "params": { "properties": ["title", "album", "artist", "duration", "thumbnail", "file", "fanart", "streamdetails"], "playerid": 0 }, "id": 1}', //this is audio player
  success: function(result, textStatus, XMLHttpRequest) { console.log(result) },
  dataType: 'json',
  contentType: 'application/json'
});
As I currently can't display info I pull. Would the following work just the same:
Code:
$.ajax({
  type: 'POST',
  url: '/jsonrpc?awxi',
  data: '{"jsonrpc": "2.0", "method": "Input.Home",  "id": "1"}',
  success: function(result, textStatus, XMLHttpRequest) { console.log(result) },
  dataType: 'json',
  contentType: 'application/json'


(2012-09-11, 23:21)Montellese Wrote: I don't see anything obviously wrong (but it has been 5-6 years since I last wrote javascript myself) apart from defining the json-rpc request in the "params" variable and then not using it in xmlhttp.send(). Do you get any error? Can you post the debug log of XBMC because it will show any incoming JSON-RPC request.
I originally did have 'params' in the send command, but after it didn't work thought I would try inserting the whole variable
Reply
#5
(2012-09-11, 23:21)Montellese Wrote: Do you get any error? Can you post the debug log of XBMC because it will show any incoming JSON-RPC request.

I enabled Debugging, but going through the log, I'm not sure what I'm looking for...
Reply
#6
Yep. data is the command to send.
Image
AWXi - Ajax web interface. Wiki
Reply
#7
(2012-09-12, 11:54)Mizaki Wrote: Yep. data is the command to send.

Within the GUI of the App, I utilize the function and command within a button press.
ie, I set the button to a Javascript function and give it a tag of xbmc:Action(Left) which would translate to my function xbmc with command Action(Left)

Would I have to define a function with JQuery?
Reply
#8
Tbh, no idea. We are flying blind here. If we were talking in the browser I'd know where we were (more or less). Can you not find any examples using jquery/JS for the HA? You might be better asking on their forums, if they have any.

Might be an idea to open xbmc, try a command and then pastebin the xbmc.log file.
Image
AWXi - Ajax web interface. Wiki
Reply
#9
(2012-09-12, 13:23)Mizaki Wrote: Tbh, no idea. We are flying blind here. If we were talking in the browser I'd know where we were (more or less). Can you not find any examples using jquery/JS for the HA? You might be better asking on their forums, if they have any.

Might be an idea to open xbmc, try a command and then pastebin the xbmc.log file.

Got it!

Thanks for all your help.
The damage an extra curly brace can do..... Should have dumped the script in Dreamweaver earlier.

Now to find a way to parse the information and thumbnail for Currently Playing item.

Thanks again.
Reply
#10
Dreamweaver < notepad++ Wink
Image
AWXi - Ajax web interface. Wiki
Reply
#11
(2012-09-12, 23:37)Mizaki Wrote: Dreamweaver < notepad++ Wink

LOL! I was using the HA Internal Script editor....
Reply
#12
Code:
function xbmcjson(command){
            
    var xmlhttp=new XMLHttpRequest();
            
            var url = "http://192.168.1.9:80/jsonrpc";
            var params = '{"jsonrpc": "2.0", "method": "'+ command +'",  "id": "1"}';
            
            //  {"jsonrpc":"2.0","id": 3,"method":"Input.ExecuteAction", "params": {"action": "left"}, "id": "1"}
        //  {"jsonrpc": "2.0", "method": "VideoLibrary.Scan", "id": "1"}
            
            // NOT Needed - creates popup on itouch with OK response
            /*xmlhttp.onreadystatechange = function() {        //Call a function when the state changes.
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    alert(xmlhttp.responseText);
                    
                }
                
            }*/
            xmlhttp.open("POST",url,true);
                        xmlhttp.setRequestHeader("Content-type","application/json");
                        xmlhttp.send(params);
            
}


Got it working with javascript and found out through a lot of digging how to post Info and Thumbnails onto the HA's GUI page.

If I send a JSON request such as:
Code:
{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}
Returns:
Code:
{"id":1,"jsonrpc":"2.0","result":[{"playerid":0,"type":"audio"}]}

What would be the easiest way to parse the info to grab the "type" element?


Reply
#13
You should get an object back. For example: In the above we should get an object called "result". As you can see from your result there is also a "result". So:
Code:
result.result.type
will give you "audio" in this case. jQuery will automatically parse it into an object from the JSON.
Image
AWXi - Ajax web interface. Wiki
Reply
#14
(2012-09-15, 23:28)Mizaki Wrote: You should get an object back. For example: In the above we should get an object called "result". As you can see from your result there is also a "result". So:
Code:
result.result.type
will give you "audio" in this case. jQuery will automatically parse it into an object from the JSON.

Apologies!

I quoted the incorrect code. Couldn't get jQuery working and was advised that it wasn't possible to use POST to achieve what I wanted by the HA gurus.
They advised I use Javascript with XMLHttpRequest.

Unfortunately now I can't capture the response, I have tried utilizing:
Code:
alert(xmlhttp.responseText);
document.getElementById("bglabel5").innerHTML = xmlhttp.responseText;
both return an empty result.

The alert function works fine at the xmlhttp.onreadystatechange and shows the response text,
but when placing it after the xmlhttp.send(params); command it returns null.

Not sure where I'm going wrong.
Reply
#15
I've never had to do it that way. Have you looked at http://www.w3schools.com/xml/xml_parser.asp etc.?
Image
AWXi - Ajax web interface. Wiki
Reply

Logout Mark Read Team Forum Stats Members Help
JSON and javascipt0