Viv*_*dra 5 jquery bugzilla jsonp web-services
这是我试过的东西......
<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>
Run Code Online (Sandbox Code Playgroud)
阅读这篇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.
Run Code Online (Sandbox Code Playgroud)
根据我的理解,"Access-Control-Allow-Origin"是由于"同源策略"问题引起的,因此我应该使用"jsonp".但是,Jsonp - 即脚本注入只能通过GET请求来完成.但是,如果我使用GET请求尝试相同的JS脚本 - 我得到以下内容:
code: 32610
message: "For security reasons, you must use HTTP POST to call the 'User.login' method."
Run Code Online (Sandbox Code Playgroud)
对于如何通过Web服务进行连接/登录感到困惑,我显然在做一些愚蠢的事情,这里有一些重大错误..如果有人可以帮助我连接并获取错误详细信息,将会提供很多帮助.我从那时起就一直在这里现在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",
...
Run Code Online (Sandbox Code Playgroud)
你必须这样做,因为:
Access-Control-Allow-Origin
你必须通过jsonp(或代理,但jsonp更简单)的事情GET
请求,而不是POST
相关文档:
POST
呼叫.Bugzilla_login
和Bugzilla_password