这是我迄今为止尝试过的内容:
<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 服务器
其他链接:
myObject
让jQuery处理 - 我会得到错误=>code: 32000 message:“无法将'params'参数解析为有效的JSON。错误:畸形JSON字符串,不是数组、对象、数字、字符串或原子,在字符偏移1之前(在“object Object”])Bugzilla/WebService/Server/JSONRPC.pm第169行。值:[object Object]"
,但我提供的json字符串符合规定的方法。如果在发送请求之前包含toJson
方法 - 就会出现请在发送请求之前包含一个方法
错误。 - Vivek Chandrajson.stringify
把它变成一个字符串 - 这是URI编码的?因此它起作用了?我不知道URI编码,得去研究一下。如果那就是我犯的错误!该死.. 我应该更仔细地读文档.. :( - Vivek ChandraJSON.stringify
将其转换为 JSON 字符串,jQuery 自动对data
对象进行 URI 编码。 - blahdiblah