2

好的,这是一个棘手的问题,但我认为我在这里遗漏了一些非常简单的东西。

目标

我正在尝试使用XmlRpcPlugin到Trac API的 JSON 接口从另一个域创建到Trac的接口。由于 Javascript 的Same Origin Policy,我使用 PHP 服务器端脚本作为代理。


地位

代理似乎(大部分)正在工作,因为我确实在 jQuery 驱动的界面中从 Trac 收到了响应。但是,我总是得到这样的回应:

{
  "error": {
    "message": "JsonProtocolException details : No JSON object could be decoded",
    "code": -32700,
    "name": "JSONRPCError"
  },
  "result": null,
  "id": null
}

这些是我的 Trac 日志中与每个请求对应的两个条目:

2012-03-05 11:37:55,943 Trac[json_rpc] ERROR: RPC(json) decode error
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/TracXMLRPC-1.1.2_r11148-py2.6.egg/tracrpc/json_rpc.py", line 148, in parse_rpc_request
    data = json.load(req, cls=TracRpcJSONDecoder)
  File "/usr/lib/python2.6/json/__init__.py", line 267, in load
    parse_constant=parse_constant, **kw)
  File "/usr/lib/python2.6/json/__init__.py", line 318, in loads
    return cls(encoding=encoding, **kw).decode(s)
  File "/usr/local/lib/python2.6/dist-packages/TracXMLRPC-1.1.2_r11148-py2.6.egg/tracrpc/json_rpc.py", line 99, in decode
    obj = json.JSONDecoder.decode(self, obj, *args, **kwargs)
  File "/usr/lib/python2.6/json/decoder.py", line 319, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.6/json/decoder.py", line 338, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

2012-03-05 11:37:55,943 Trac[web_ui] ERROR: RPC(JSON-RPC) Error
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/TracXMLRPC-1.1.2_r11148-py2.6.egg/tracrpc/web_ui.py", line 143, in _rpc_process
    rpcreq = req.rpc = protocol.parse_rpc_request(req, content_type)
  File "/usr/local/lib/python2.6/dist-packages/TracXMLRPC-1.1.2_r11148-py2.6.egg/tracrpc/json_rpc.py", line 162, in parse_rpc_request
    raise JsonProtocolException(e, -32700)
JsonProtocolException: No JSON object could be decoded

代码

我的 jQuery 代码:

$.ajax({
    url: "/ajax/trac_proxy.php",
    dataType: "json",
    data: {"method": "system.listMethods", "id": 1},
    timeout: 5000,
    type: 'POST',
    success: function(data, status, XHR){
        alert(JSON.stringify(data));
    }
});

我的 PHP 脚本(/ajax/trac_proxy.php为清楚起见缩短了):

<?php
    $cparams = array(
        'http' => array(
            'method' => 'POST',
            'ignore_errors' => true,
            'content' => http_build_query($_POST),
            'header' => "Authorization: Basic " . base64_encode($username . ':' . $password) . "\r\n"
                      . "Content-Type: application/json\r\n"
        )
    );
    $context = stream_context_create($cparams);
    $fp = fopen('https://mytracdomain.com/login/jsonrpc', 'rb', false, $context);
    echo stream_get_contents($fp);
?>

调试结果

要查看 PHP 正在做什么/做什么,我更改/ajax/trac_proxy.php为:

$cparams = array(
    'http' => array(
        'content' => http_build_query($_POST),
        'method' => 'POST',
        'ignore_errors' => true,
        'header' => "Authorization: Basic " . base64_encode($username . ':' . $password) . "\r\n"
                  . "Content-Type: application/json\r\n"
    )
);

var_dump($_POST);
var_dump(http_build_query($_POST));
var_dump($cparams);

这是我回来的:

array(2) {
  ["method"]=>
  string(18) "system.listMethods"
  ["id"]=>
  string(1) "1"
}

string(30) "method=system.listMethods&id=1"

array(1) {
  ["http"]=>
  array(4) {
    ["content"]=>
    string(30) "method=system.listMethods&id=1"
    ["method"]=>
    string(4) "POST"
    ["ignore_errors"]=>
    bool(true)
    ["header"]=>
    string(79) "Authorization: Basic <REMOVED>
Content-Type: application/json
"
  }
}

环境

  • 追踪Trac 0.11.7
  • 蟒蛇Python 2.6
  • 网络服务器Apache 2.2.14
  • 操作系统Ubuntu 10.04

我究竟做错了什么?

4

2 回答 2

1

弄清楚了。

问题content$cparams.

http_build_query()返回一个 URL查询字符串。JSON-RPC 插件所期望的是一个JSON 字符串(想象一下),它很json_encode()容易提供。

改变这个:

$cparams = array(
    'http' => array(
        'content' => http_build_query($_POST),
        'method' => 'POST',
        'ignore_errors' => true,
        'header' => "Authorization: Basic " . base64_encode($username . ':' . $password) . "\r\n"
                  . "Content-Type: application/json\r\n"
    )
);

对此:

$cparams = array(
    'http' => array(
        'content' => json_encode($_POST),
        'method' => 'POST',
        'ignore_errors' => true,
        'header' => "Authorization: Basic " . base64_encode($username . ':' . $password) . "\r\n"
                  . "Content-Type: application/json\r\n"
    )
);

解决它。

于 2012-03-05T19:39:30.137 回答
1

有一个不错的用于 PHP 的 Trac JSON-RPC 库,它可以帮助您构建正确的请求。

https://github.com/bgreenacre/TRAC-JSON-RPC-PHP-class

于 2012-03-05T18:23:30.203 回答