JSONRPC via jQuery
#1
Yeah, I'm really new to the whole JSONRPC world, and I'm trying to get it working using jQuery. What I'm doing is:

Code:
    $("#clickme").click(function(event){
        console.log( "clicked!" );
        event.preventDefault();

        var urlx = "http://xbmc:8080/jsonrpc";
        
        var request = {};
        request.method = "JSONRPC.Introspect";
        request.type = "namespace";
        request.id = "Introspect";
        request.jsonrpc = "2.0";
        
        function goodFN(response)
        {
            console.log( "good!" );
            console.log( response );
        };

        function errFN( jq, text, thrown )
        {
            console.log( "error!" );
            console.log( jq );
            console.log( text );
            console.log( thrown );
        };

        $.ajax(
            {
                type    : "POST",
                url     : urlx,
                data    : JSON.stringify(request),
                dataType: "text",
                error   : errFN,
                success : goodFN
            }
        );

    });

What I'm getting is a call to the error() method, with jq of:

Code:
Object { readyState=0, status=0, statusText="error"}

and text/thrown of "error" and an empty string, respectively.

In Firebug, I see response headers of:

Code:
Content-Length    23780
Content-Type    application/json
Date    Thu, 29 Dec 2011 02:54:01 GMT

and get a POST result of 200 OK, but then I don't get anything back that makes sense.

Seems like XBMC is returning what it should, but jQuery or Firebuf, one, are throwing their hands up and not showing what's coming back.

Is there something obvious that I should be doing to see the reply?

Thanks lots,
Dave
Reply
#2
I dunno if this'll help but this is how AWX does it:
Code:
        sendCommand: function(command, onSuccess, onError, onComplete, asyncRequest) {
            if (typeof asyncRequest === 'undefined')
                asyncRequest = true;

            if (!this.xbmcHasQuit) {
                $.ajax({
                    async: asyncRequest,
                    type: 'POST',
                    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) {
                        //console.log(onComplete);
                        //if (onComplete) { onComplete(); }
                    }
                });
            }
        }

Then you call like:
Code:
        getAudioGenres: function(options) {
            var settings = {
                onSuccess: null,
                onError: null
            };
            $.extend(settings, options);

            xbmc.sendCommand(
                '{"jsonrpc": "2.0", "method": "AudioLibrary.GetGenres", "id": 1}',

                function(response) {
                    settings.onSuccess(response.result);
                },

                settings.onError
            );
        }

Used in page:
Code:
            xbmc.getGenresAlbums({
                genreid: e.data.idGenre,

                onError: function() {
                    mkf.messageLog.show(mkf.lang.get('message_failed_album_list'), mkf.messageLog.status.error, 5000);
                    $artistGenresContent.removeClass('loading');
                },

                onSuccess: function(result) {
                    $artistContent.defaultAlbumViewer(result, artistPage);
                    $artistContent.removeClass('loading');
                }
            });
Image
AWXi - Ajax web interface. Wiki
Reply
#3
Crap, crap, crap. $#!&. It was a cross-domain issue. Put my code up on my ATV, and it worked fine. When am I gonna learn...

Thanks, Mizaki, comparing code was what got me thinking in the right direction.

Dave
Reply

Logout Mark Read Team Forum Stats Members Help
JSONRPC via jQuery0