0

I'm trying to access some data through JSON RPC using the trac XMLRPCPlugin using my own method 'plugged' into one of it's extension points on the server side and a JQuery AJAX request on the client side. I can access the data fine using the Firefox poster extension however using the JQuery ajax request gives an error message. The error message I get is:

Trac[web_ui] DEBUG: RPC incoming request of content type 'application/json' dispatched 
to <tracrpc.json_rpc.JsonRpcProtocol object at 0x03CA51F0>
Trac[web_ui] DEBUG: RPC(JSON-RPC) call by 'PaulWilding'
Trac[json_rpc] ERROR: RPC(json) decode error 
Traceback (most recent call last):
  File "build\bdist.win32\egg\tracrpc\json_rpc.py", line 148, in parse_rpc_request
    data = json.load(req, cls=TracRpcJSONDecoder)
  File "C:\Python27\Lib\json\__init__.py", line 278, in load
    **kw)
  File "C:\Python27\Lib\json\__init__.py", line 339, in loads
    return cls(encoding=encoding, **kw).decode(s)
  File "build\bdist.win32\egg\tracrpc\json_rpc.py", line 99, in decode
    obj = json.JSONDecoder.decode(self, obj, *args, **kwargs)
  File "C:\Python27\Lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\Lib\json\decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
Trac[web_ui] ERROR: RPC(JSON-RPC) Error
Traceback (most recent call last):
  File "build\bdist.win32\egg\tracrpc\web_ui.py", line 143, in _rpc_process
    rpcreq = req.rpc = protocol.parse_rpc_request(req, content_type)
  File "build\bdist.win32\egg\tracrpc\json_rpc.py", line 162, in parse_rpc_request
    raise JsonProtocolException(e, -32700)
JsonProtocolException: No JSON object could be decoded
Trac[json_rpc] DEBUG: RPC(json) encoded response: {"error": {"message": "JsonProtocolException details : No JSON object could be decoded", "code": -32700, "name": "JSONRPCError"}, "result": null, "id": null}

The JQuery request is:

 url: "http://localhost/Projects/jsonrpc",
   contentType: "application/json",
   dataType: "jsonp",
   data: {"method": "breq.getBreqs"},
   type: 'POST',
   success: function (repsonse) {
          alert("success");
   },
   error: function (jqXHR, textStatus, errorThrown) {
          alert("Error: " + textStatus);
   }      

The request I’ve got working through Poster is simply “{"method": "breq.getBreqs"}” with content set to application/json and the URL the same as above.

I’ve read a few posts about this problem when used with a PHP proxy and tried logging the request in the parse_rpc_request in the trac rpc plugin but this only returned the same string of “” for both the working and non-working requests.

I don't think this is a problem due to Javascript's Same Origin Policy as the request is to my trac testbed which is on my machine. However, once the plugin is deployed onto a server still inside the main network will this become an issue and if so, how should I deal with it?

If the problem isn't to do with the Same Origin Policy does anyone know what is causing it?

pwilding
  • 33
  • 1
  • 6

2 Answers2

0

The problem may be in what you are actually sending as a parameter:

data: {"method": "breq.getBreqs"}

Try the solution described here: Jquery Ajax Posting json to webservice

Community
  • 1
  • 1
  • Thanks Roberto, that seems to have made some progress but I now have a different error. My data field now looks like this: `data: JSON.stringify({"method": "breq.getBreqs"})`. I'll post the error message below as there isn't enough space in here! – pwilding Jul 26 '12 at 08:57
  • `ValueError: Extra data: line 1 column 1 - line 1 column 133 (char 1 - 133) Trac[web_ui] ERROR: RPC(JSON-RPC) Error Traceback (most recent call last): File "build\bdist.win32\egg\tracrpc\web_ui.py", line 143, in _rpc_process rpcreq = req.rpc = protocol.parse_rpc_request(req, content_type)` – pwilding Jul 26 '12 at 09:01
  • `File "build\bdist.win32\egg\tracrpc\json_rpc.py", line 162, in parse_rpc_request raise JsonProtocolException(e, -32700) JsonProtocolException: Extra data: line 1 column 1 - line 1 column 133 (char 1 - 133) Trac[json_rpc] DEBUG: RPC(json) encoded response: {"error": {"message": "JsonProtocolException details : Extra data: line 1 column 1 - line 1 column 133 (char 1 - 133)", "code": -32700, "name": "JSONRPCError"}, "result": null, "id": null} ` – pwilding Jul 26 '12 at 09:02
  • I've managed to log the data being sent and it looks like this: `0=%7B&1=%22&2=m&3=e&4=t&5=h&6=o&7=d&8=%22&9=%3A&10=%22&11=b&12=r&13=e&14=q&15=.&16=g&17=e&18=t&19=B&20=r&21=e&22=q&23=s&24=%22&25=%7D` . Presumably this should be JSON rather than this so is there a particular thing that is causing this? – pwilding Jul 26 '12 at 11:48
0

Your call will URL encode the input, which is not what you want for JSON-RPC. You want the request body to be a JSON string with no added characters or information.

Here is a new version of your snippet that loads fine from a browser javascript console, but note that my example calls system.listMethods (and not your custom method) so that it works for any Trac RPC installation:

$.ajax({
   url: "http://localhost/trac/rpc",
   contentType: "application/json",
   dataType: "text",
   data: JSON.stringify({method: "system.listMethods", id: 42}),
   type: 'POST',
   success: function (response) {
       incoming = JSON.parse(response)
       alert("Result ID " + incoming["id"] + ": " + incoming["result"]);
   },
   error: function (jqXHR, textStatus, errorThrown) {
       alert("Error: " + textStatus);
   }
});

Use /login/rpc URL unless anonymous have assigned XML_RPC permission.

osimons
  • 183
  • 1
  • 3