Can't get a response with JSON-RPC and JavaScript
#1
Hi there,

I've been trying to follow the working code in this thread http://forum.xbmc.org/showthread.php?tid=140310 but sadly with no success.

I'm completely new to JSON-RPC and haven't written much javascript either, but as I've been able to get responses playing around in C# and Python, I'm convinced the flaw lies somewhere in my Javascript, even through it's basically lifted directly from the above thread.

The other thing that's confusing me is that running the debug log on my xbmc server shows no JSON requests at all, not even failed ones. These requests definitely do show up when I send them from other sources

From what I've read, I think the following basic web-page should enable me to call the JSONRPC.Introspect method, and then the response should pop up in a window alert (Yes, I'm really falling at the first hurdle).

Code:
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
</head>
<body>
<script>
  function thecallback(a, b, c) {
    alert(a);
  }

  function xbmcjson(){
    
    var params = '{"jsonrpc": "2.0", "method": "JSONRPC.Introspect",  "id": "1"}';
    

    $.ajax({
            type: 'POST',
            url: 'http://192.168.0.10:9080/jsonrpc',
            data: params,
            success: thecallback(result, textStatus, XMLHttpRequest),
            dataType: 'json',
            contentType: 'application/json'
    });
}
</script>

<button onclick="xbmcjson()">Try it</button>

</body>
</html>

Any help at all would be tremendously appreciated.
Reply
#2
F12 in browser to see the error Smile

Code:
success: function(result, textStatus, XMLHttpRequest) {
              alert(result);
            },

Instead of getting too fancy with callbacks right away. Gives me a x-domain error in chrome but it's closer.

In AWXi it's
Code:
sendCommand: function(command, onSuccess, onError, onComplete, asyncRequest) {
      if (typeof asyncRequest === 'undefined')
        asyncRequest = true;

      if (!this.xbmcHasQuit) {
        $.ajax({
          async: asyncRequest,
          type: 'POST',
          contentType: 'application/json',
          url: '/jsonrpc?awx',
          data: command,
          dataType: 'json',
          cache: false,
          timeout: this.timeout,
          success: function(result, textStatus, XMLHttpRequest) {

            // its possible to get here on timeouts. --> error
            if (XMLHttpRequest.readyState==4 && XMLHttpRequest.status==0) {
              if (onError) {
                onError({"error" : { "ajaxFailed" : true, "xhr" : XMLHttpRequest, "status" : textStatus }});
              }
              return;
            }

            // Example Error-Response: { "error" : { "code" : -32601, "message" : "Method not found." } }
            if (result.error) {
              if (onError) { onError(result); }
              return;
            }

            if (onSuccess) { onSuccess(result); }
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            if (onError) {
              onError({"error" : { "ajaxFailed" : true, "xhr" : XMLHttpRequest, "status" : textStatus, "errorThrown" : errorThrown }});
            }
          },
          complete: function(XMLHttpRequest, textStatus) {
            //if (onComplete) { onComplete(); }
          }
        });
      }
    }
Which might help you.
Image
AWXi - Ajax web interface. Wiki
Reply
#3
Hi Mizaki,

Thanks for the response. In Chrome I was getting that x-domain error, but I've been testing in Firefox at the moment which doesn't report any such error. All I get there (with my original code) is "Reference Error: result is not defined".

I've put in your suggested change, and now in Firefox if I open the error console I see that when I click my button, I get a
Code:
[20:57:15.202] OPTIONS http://192.168.0.10:9080/jsonrpc [HTTP/1.1 200 OK 4ms]

So for some reason the method isn't using POST, I think this is what's going wrong. Googling about a little suggests that this is also something to do with x-domain usage of POST. I'm going to keep investigating, but once again if anyone has any suggestions how to get around this I'd be grateful.

I'm a little closer to the solution, though, so thanks again.
Reply
#4
It's just occurred to me. Is the whole issue here that I'm running my test-page on a different web-server from the box that XBMC is running on?
Reply
#5
It's run client side. If you are using Firefox try FireBug and then you can have a better look at the network traffic or turn it on for default FF console. A restful client for FF might be helpful as well.
Image
AWXi - Ajax web interface. Wiki
Reply
#6
Thanks Mizaki. I'd already used Firebug and the built-in tools to see what was in the request. I've used the recommended add-on to send POSTs in Chrome without any problems, so I think the problem is simply that what I'm attempting to do is slightly frowned upon by browsers for security reasons.

Everything I've read so far seems to suggest that this is a browser security measure, in that they won't let me POST from a different domain, and that includes from one port to another, so the fact that I have a webserver at port 80 on the machine trying to post to XBMC at port XXXX means Firefox and Chrome won't allow it, and change the requests into OPTIONS only. I see that XBMC intends to support the jsonp soon, which should solve the cross-domain problem.

I think I can get around this by using php to do the actual POST and then return the results that way. Will feed back on how I get on!
Reply
#7
Hello again.

In the end, what I've done is use a php page to handle the actual POSTing, and just retrieved the results using ajax.

It's perhaps not as elegant as doing everything in JS and jQuery, but it does work.

If anyone is having a similar problem, I've used the following page to dispatch the POST requests for me, and then return the results for me to handle in my javascript

PHP POST with cURL

Thanks again for the help
Reply

Logout Mark Read Team Forum Stats Members Help
Can't get a response with JSON-RPC and JavaScript0