好的,这是一个棘手的问题,但我认为我在这里遗漏了一些非常简单的东西。
目标
我正在尝试使用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
我究竟做错了什么?