[英]Problems with JQuery $.ajax request to random.org api
I am having issues with the api and I've tried lots of things and nothing seems to be working. 我遇到了api的问题,我尝试了很多东西,似乎没有任何工作。 I used their https://api.random.org/json-rpc/1/request-builder JSON object creator to make sure I was doing the JSON correctly.
我使用他们的https://api.random.org/json-rpc/1/request-builder JSON对象创建器来确保我正确地执行了JSON。 I have looked over my JSON request and I think the issue lays there.
我查看了我的JSON请求,我认为问题就在那里。
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. 例如,关键是0。
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.
我必须发送这些数据..不是它也想要我,我相信这与我的JSON请求标头有关。
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? 我知道我可以使用Maths.random(),或者可能会翻录整个演示页面,但是他们使用了一堆HTML表单,这应该只是简单的Ajax ..对吗?
You need to stringify the data yourself if you want to send as json 如果要以json格式发送,则需要自己对数据进行字符串化
$.ajax default is to form encode objects $ .ajax默认是形成编码对象
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);
}
});
I added ticks (``) to stringify the object and double quotations ("") where there were singles. 我添加了ticks(``)来字符串化对象和双引号(“”),其中有单打。 Adding my solution for other random.org users:
为其他random.org用户添加我的解决方案:
$.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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.