Userscript AJAX request gives JSON-RPC error -32700 -


this extremely similar issues, eg: request random.org api zendframework2.
however, think problem lies somewhere in 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 problem in json request. i'm hoping more experience json-rpc can point me in right direction.

you need json encode 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);     } } ); 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -