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 in Web Development by gaurav
• 17,580 points
16 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 by rajatha
• 7,040 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

5 days ago in Web Development by gaurav
• 17,580 points
9 views
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,740 points
5,987 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,740 points
5,611 views
0 votes
1 answer

Using Jquery Ajax to retrieve data from Mysql

Perform a AJAX GET request to get ...READ MORE

answered Jun 23 in Web Development by rajatha
• 7,040 points
54 views
0 votes
1 answer

jQuery AJAX submit form

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

answered Feb 8 in Java by Soham
• 8,740 points
255 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 in Others by Aditya
• 7,300 points
483 views
0 votes
0 answers

How can I upload files asynchronously with jQuery?

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

Jun 10 in JQuery by gaurav
• 17,580 points
36 views
0 votes
1 answer

Abort Ajax requests using jQuery

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

answered Jun 16 in JQuery by rajatha
• 7,040 points
35 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 in Web Development by rajatha
• 7,040 points
13 views
0 votes
1 answer

How to use JQuery with ReactJS

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

answered Jun 22 in Web Development by rajatha
• 7,040 points
16 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP