Problems with JQuery ajax request to random org api

0 votes

I am having issues with the api and I've tried lots of things and nothing seems to be working. I used their https://api.random.org/json-rpc/1/request-builder JSON object creator to make sure I was doing the JSON correctly. I have looked over my JSON request and I think the issue lays there.

Here is my code:

$.ajax({
            url: 'https://api.random.org/json-rpc/1/invoke',
            type:"POST",
            data:{
                'jsonrpc': '2.0',
                'method': 'generateIntegers',
                'params': {
                    'apiKey': '00000000-0000-0000-0000-000000000000',
                    'n': 10,
                    'min': 1,
                    'max': 10,
                    'replacement': true,
                    'base': 10
                },
                'id': 2601
            },
            contentType:"application/json; charset=utf-8",
            dataType:"json",
            success: function(result){
                $('#text').html(JSON.stringify(result));
                console.log(result);
                }
            }); 

The key is 0'ed out for example.

The response I get is this:

{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error","data":null},"id":null}

And the websites error code sheet says the message is due to parsing error, and the data I grabbed is sure to be correct so that just leaves it down to.. I'm sending it wrong? I must be sending this data .. not how it wants me too and I believe that has something to do with my JSON request headers.

I know I could just use Maths.random(), or probably rip their entire demonstration page, but they use a bunch of HTML forms and this should just be simple Ajax.. Right?

Aug 1, 2022 in Web Development by gaurav
• 23,260 points
1,046 views

1 answer to this question.

0 votes

You need to stringify the data yourself if you want to send as json

$.ajax default is to form encode objects

Try

var data: {
  'jsonrpc': '2.0',
  'method': 'generateIntegers',
  'params': {
    'apiKey': '00000000-0000-0000-0000-000000000000',
    'n': 10,
    'min': 1,
    'max': 10,
    'replacement': true,
    'base': 10
  },
  'id': 2601
};

$.ajax({
  url: 'https://api.random.org/json-rpc/1/invoke',
  type: "POST",
  data: JSON.stringify(data),// stringify data object
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(result) {
    $('#text').html(JSON.stringify(result));
    console.log(result);
  }
});
answered Aug 1, 2022 by rajatha
• 7,680 points

Related Questions In Web Development

0 votes
0 answers

How to obtain data from random wikipedia page with jquery

$(".wikiResult h3").load("https://en.wikipedia.org/wiki/Special:Random #firstHeading i"); I would like to ...READ MORE

Aug 11, 2022 in Web Development by gaurav
• 23,260 points
518 views
0 votes
0 answers
0 votes
1 answer

How to replace html element with ajax response?

Hello @kartik, Assuming you are replacing your products, ...READ MORE

answered Jul 8, 2020 in Web Development by Niroj
• 82,840 points
10,576 views
0 votes
1 answer

How to download a file by jQuery.Ajax?

Hello @kartik, You don't need to do this ...READ MORE

answered Sep 18, 2020 in Web Development by Niroj
• 82,840 points
7,508 views
0 votes
1 answer

jQuery AJAX submit form

There is a simple input mentioned below ...READ MORE

answered Feb 8, 2022 in Java by Soham
• 9,710 points
1,649 views
0 votes
1 answer

TypeError: $.ajax(...) is not a function?

Please double-check if you're using the full-version ...READ MORE

answered Feb 22, 2022 in Others by Aditya
• 7,680 points
6,943 views
0 votes
0 answers

How can I upload files asynchronously with jQuery?

I would like to upload a file ...READ MORE

Jun 10, 2022 in JQuery by gaurav
• 23,260 points
651 views
0 votes
1 answer

Abort Ajax requests using jQuery

Instead of aborting, you can choose to ...READ MORE

answered Jun 16, 2022 in JQuery by rajatha
• 7,680 points
764 views
0 votes
1 answer

How to cast jQuery $.ajax calls to Bluebird promises without the deferred anit-pattern

jQuery have promises implemented with their AJAX ...READ MORE

answered Aug 1, 2022 in Web Development by rajatha
• 7,680 points
541 views
0 votes
1 answer

How to use JQuery with ReactJS

Yes, we can use jQuery in ReactJs. ...READ MORE

answered Jun 22, 2022 in Web Development by rajatha
• 7,680 points

edited Dec 15, 2023 by Soumya 14,728 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP