[英]Userscript AJAX request gives JSON-RPC error -32700
This is extremely similar to issues, EG: Request to Random.org API with ZendFramework2 . 这与问题(例如EG: 使用ZendFramework2向Random.org API的请求 )极为相似。
However, I think my problem lies somewhere in my data request. 但是,我认为我的问题出在我的数据请求中。
GM_xmlhttpRequest({
method: "POST",
url: "https://api.random.org/json-rpc/1/invoke",
data: {
"jsonrpc": "2.0",
"method": "generateDecimalFractions",
"params": "{\"apiKey\": \"d2319b89-8389-4d24-b1eb-4dbd80009153\",\"n\": 10,\"decimalPlaces\": 8,\"replacement\": true}",
"id": 15324815
},
headers:{
"Content-Type": "application/json-rpc"
},
onload: function(response) {
console.log(response);
}
});
The error I'm getting is: 我得到的错误是:
finalUrl: "https: //api.random.org/json-rpc/1/invoke"
finalUrl:“ https://api.random.org/json-rpc/1/调用”
readyState: 4readyState:4
response: "{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error", "data": null}, "id": null}"响应:“ {” jsonrpc“:” 2.0“,” error“:{” code“:-32700,” message“:” Parse error“,” data“:null},” id“:null}”
responseHeaders: "Date: Wed, 01 Apr 2015 02: 34: 08 GMT?Server: Apache/2.2.22 (Debian)?X-Powered-By: PHP/5.4.39-0+deb7u2?Content-Type: application/json; charset=utf-8?Access-Control-Allow-Origin:responseHeaders:“日期:2015年4月1日星期三02:34:08 GMT?服务器:Apache / 2.2.22(Debian)?X-Powered-By:PHP / 5.4.39-0 + deb7u2?Content-Type:application / json; charset = utf-8?Access-Control-Allow-Origin:
*?Connection: Keep-Alive?Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept?Content-Length: 87?Keep-Alive: timeout=15, max=200?"*“连接:保持活动状态”访问控制允许标题:来源,X请求的内容,内容类型,接受?内容长度:87?保持活动状态:超时= 15,最大值= 200?”
responseText: "{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error", "data": null}, "id": null}"responseText:“ {” jsonrpc“:” 2.0“,” error“:{” code“:-32700,” message“:” Parse error“,” data“:null},” id“:null}”
responseXML: nullresponseXML:空
status: 200状态:200
statusText: "OK"statusText:“确定”
So I'm fairly certain the problem is in my JSON request. 因此,我可以肯定地确定问题出在我的JSON请求中。 I'm hoping that someone with more experience with JSON-RPC can help point me in the right direction.
我希望对JSON-RPC有更多经验的人可以帮助我指出正确的方向。
You need to properly JSON encode the data before sending it: 在发送数据之前,您需要对数据进行JSON正确编码:
var encodedData = JSON.stringify ( {
"jsonrpc": "2.0",
"method": "generateDecimalFractions",
"params": {apiKey: "d2319b89-8389-4d24-b1eb-4dbd80009153", n: 10, decimalPlaces: 8, replacement: true},
"id": 15324815
} );
GM_xmlhttpRequest ( {
method: "POST",
url: "https://api.random.org/json-rpc/1/invoke",
data: encodedData,
headers: {
"Content-Type": "application/json"
},
onload: function (response) {
console.log (response);
}
} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.