这是我到目前为止所尝试过的..
<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 - 即脚本注入(inject)只能通过 GET 请求完成。但是,如果我使用 GET 请求尝试相同的 JS 脚本 - 我会得到以下结果:
code: 32610
message: "For security reasons, you must use HTTP POST to call the 'User.login' method."
对如何通过网络服务连接/登录感到困惑,我显然在这里做了一些愚蠢的事情,这是一个重大错误。如果有人可以帮助我连接并获取错误详细信息,那将是一个很大的帮助。我一直在已经用了 8-10 天了..:(
仅供引用:
我无权访问服务器
我正在设置客户端并访问 Bugzilla 服务器
其他链接,
最佳答案
您需要使用 Bugzilla_login
和 Bugzilla_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
相关文档:
- Connecting via GET - 您检索信息的能力将受到限制,更改需要
POST
调用。 - Logging in via
Bugzilla_login
andBugzilla_password
关于jquery - Bugzilla - 通过 JSON-RPC 的 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11667809/