Bugzilla - 通过 JSON-RPC 进行的 Web 服务

5

这是我迄今为止尝试过的内容:

<html>
  <head>
    <title>bugstats.com</title>
  </head>
<script type="text/javascript"     src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://jquery-json.googlecode.com/files/jquery.json-    1.3.min.js"></script>
<script type="text/javascript" >
function hello(){
var myObject = {"method":"User.login", /* is this the right method to call? */
"params":[  { "login" :"user", /*should i include the login credentials here? */
"password" : "pass123" , 
"remember" : "True"} ]  };
var enc = $.toJSON(myObject);

$.ajax({"contentType":"application/json",
    "data": enc, 
    "crossDomain":"true",
    "dataType": "json", 
    "url": "https://bugzilla.company.com/bugzilla/jsonrpc.cgi", /* is this correct or should it be https://bugzilla.company.com/bugzilla/jsonrpc.cgi?method=User.login? */ 
    "type": "POST",
    success: function(){
            alert("Hallelujah");
                console.log(arguments); 

             },
    error: function () {
    alert("Failed")
    }

   });
}
function parseResponse(obj){
 alert("Success")
 console.log(obj)
}
</script>
  <body>
    <h1>bugzilla.com</h1>
    <input type="button" onclick="hello()" value="Click">
</body>

阅读关于JSONPRC的内容,但没有进展。

当我点击按钮进行调用、登录或执行其他操作时,出现以下错误:

OPTIONS https://bugzilla.company.com/bugzilla/jsonrpc.cgi 403 (Forbidden) jquery.min.js:19
XMLHttpRequest cannot load https://bugzilla.company.com/bugzilla/jsonrpc.cgi. Origin http://172.16.229.137 is not allowed by Access-Control-Allow-Origin.

根据我的理解,“Access-Control-Allow-Origin”是因为“同源策略”的问题所造成的,因此我应该使用“jsonp”。但是,Jsonp即脚本注入只能通过GET请求来完成。但是,如果我尝试使用相同的JS脚本并发送GET请求,我会得到以下结果:

code: 32610
message: "For security reasons, you must use HTTP POST to call the 'User.login' method."

对于如何通过 Web 服务连接/登录感到困惑,我显然在做一些傻事或者是重大的错误...如果有人能帮助我连接并获取错误详细信息,那将是非常有帮助的。我已经尝试了8-10天了...:(

其他信息:

  • 我没有访问服务器的权限

  • 我正在客户端设置上访问 Bugzilla 服务器

其他链接:

Ajax 调用

登录

BugzillaApc

Google Groups - 实时对话

1个回答

5
您需要在每个调用中使用Bugzilla_loginBugzilla_password参数进行身份验证,这将是使用jsonp的GET调用。您的调用将如下所示,以User.get为例:
// Method parameters
var params = [{
  /* The authentication parameters */
  "Bugzilla_login": "YourUserName",
  "Bugzilla_password": "YourPassword",
  /* The actual method parameters */
  "ids": [1, 2]
}];
var myObject = {
  "method": "User.get",
  "params": JSON.stringify(params)
};

$.ajax({"contentType": "application/json",
    "data": myObject, /* jQuery will handle URI encoding */
    "crossDomain": "true",
    "dataType": "jsonp", /* jQuery will handle adding the 'callback' parameter */
    "url": "https://bugzilla.company.com/bugzilla/jsonrpc.cgi", 
    "type": "GET",
    ...

你需要这样做,因为:
  • 你将会进行跨域调用
  • 由于你不能设置像 Access-Control-Allow-Origin 这样的东西,所以你必须通过 jsonp 进行操作(或代理,但 jsonp 更简单)
  • jsonp 必须是一个 GET 请求,而不是 POST
相关文档:

如果我尝试上面的代码片段,即使用原始myObject让jQuery处理 - 我会得到错误=>code: 32000 message:“无法将'params'参数解析为有效的JSON。错误:畸形JSON字符串,不是数组、对象、数字、字符串或原子,在字符偏移1之前(在“object Object”])Bugzilla/WebService/Server/JSONRPC.pm第169行。值:[object Object]",但我提供的json字符串符合规定的方法。如果在发送请求之前包含toJson方法 - 就会出现请在发送请求之前包含一个方法错误。 - Vivek Chandra
@VivekChandra 哎呀,我编码有点太快了,请查看更新,应该把所有正确的编码放在正确的位置。 - blahdiblah
成功了.. :) .. 非常感谢,但有一个小问题 - 在Doc中写着甚至可以接受一个数组!所以为什么当params是一个数组时它却接受字符串而不是数组? - Vivek Chandra
@VivekChandra 数组是被接受的,但必须进行URI编码。请参考上面调用中的“ids”参数作为示例。 - blahdiblah
所以,json.stringify 把它变成一个字符串 - 这是URI编码的?因此它起作用了?我不知道URI编码,得去研究一下。如果那就是我犯的错误!该死.. 我应该更仔细地读文档.. :( - Vivek Chandra
@VivekChandra JSON.stringify 将其转换为 JSON 字符串,jQuery 自动对 data 对象进行 URI 编码。 - blahdiblah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接