2012-07-26 79 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