'Call a json_handler in the XBlock. Return the response as
an object containing response code and JSON.'

def ajax(self, function, block_urlname, json_data):

    url = self._get_handler_url(function, block_urlname)
    resp = self.client.post(url, json.dumps(json_data), '')
    ajax_response = collections.namedtuple('AjaxResponse', ['data', 'status_code'])
    try:
        ajax_response.data = json.loads(resp.content)
    except ValueError:
        print 'Invalid    JSON    response'
        print '(Often    a    redirect    if    e.g.    not    logged    in)'
        print >>sys.stderr, 'Could    not    load    JSON    from    AJAX    call'
        print >>sys.stderr, 'Status:', resp.status_code
        print >>sys.stderr, 'URL:', url
        print >>sys.stderr, 'Block', block_urlname
        print >>sys.stderr, 'Response', repr(resp.content)
        raise
    ajax_response.status_code = resp.status_code
    return ajax_response
