This is extremely similar to issues, EG: Request to Random.org API with ZendFramework2.
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"
readyState: 4
response: "{"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:
*?Connection: Keep-Alive?Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept?Content-Length: 87?Keep-Alive: timeout=15, max=200?"
responseText: "{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error", "data": null}, "id": null}"
responseXML: null
status: 200
statusText: "OK"
So I'm fairly certain the problem is in my JSON request. I'm hoping that someone with more experience with JSON-RPC can help point me in the right direction.
You need to properly JSON encode the data before sending it:
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);
}
} );