2012-07-26 62 views
5

这里Web服务是我所洙远远试过..的Bugzilla - 通过JSON-RPC

<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. 

从我的理解,“访问控制允许来源”是由于“相同的原产地政策”问题而引起的,因此我应该使用“jsonp”。但是,Jsonp - 即脚本注入只能通过GET请求完成。但是,如果我尝试同样的JS脚本的GET请求 - 我得到如下:

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

困惑如何通过Web服务连接/登录,显然我在做一些愚蠢的事情majorly错在这里..是的帮助很大,如果有人能帮助我连接,并获取自现在8-10天一直在它的bug details.I've .. :(

FYI:

  • 我做无法访问服务器

  • 我在客户端安装和访问Bugzilla的服务器

等各个环节,

Ajax Call

Loggin In

BugzillaApc

Google Groups - Live conversation

回答

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

相关的文档:

+0

如果我尝试上面的代码片段,即。原始'myObject'为jQuery处理 - 我得到了错误=>'代码:32000 message:“无法将'params'参数解析为有效的JSON错误:格式错误的JSON字符串,数组,对象,数字,字符串或原子,在字符偏移1(在“对象对象”之前)在Bugzilla/WebService/Server/JSONRPC.pm行169.值:[对象对象]“',但我给的json字符串是按照规定的方法。如果我在发送之前包含'toJson'方法 - 我在发送请求之前得到'请包括一个方法'错误出现.. – 2012-08-01 06:21:32

+0

@VivekChandra糟糕,编码起来有点过快,请看看应该有所有更新正确的编码在正确的地方。 – blahdiblah 2012-08-01 22:38:49

+0

它的工作.. :) ..感谢百万,但有一个小问题 - 在[__Doc__](http://www.bugzilla.org/docs/4.2/en/html/api/Bugzilla/WebService.html #CALLING_METHODS)它说甚至一个数组被接受!,所以为什么字符串被接受,而不是当params是一个数组?? .. – 2012-08-03 08:27:39