如何解决从在线托管的 Json 中获取 ID
我正在尝试使用托管服务为我设置的 ID 取回我在线保存的 jason 数据..
const app = new Vue({
el:'#app',data:{
input:'',method:'',status:'200',timeStart:0,timeEnd:0,},mounted(){
},methods:{
HTTP_Request(){
const url = 'https://' + this.input;
const currentDate = new Date();
this.timeStart = currentDate.getTime();
axios.get(url)
.then(res => {
let parser = document.createElement('a');
parser.href = url;
const protocol = parser.protocol;
const hostname = parser.hostname; // => "example.com"
const pathname = parser.pathname; // => "/pathname/"
const status = res.status;
const method = this.method;
console.log(status,method);
var resp = {
"status": status,"errors": {},"data": {
"method": method,"protocol": protocol,"hostname": hostname,"pathname": pathname,}
}
var response = JSON.stringify(resp);
let req = new XMLHttpRequest();
req.onreadystatechange = () => {
if (req.readyState == XMLHttpRequest.DONE) {
console.log(req.responseText);
console.log(req);
}
};
req.open("POST","https://api.jsonbin.io/v3/b",true);
req.setRequestHeader("Content-Type","application/json");
req.setRequestHeader("X-Master-Key","myapikey");
req.send(response);
const currentDate = new Date();
this.timeEnd = currentDate.getTime();
console.log((this.timeEnd-this.timeStart)/1000);
console.log(resp)
到目前为止,我可以在 https://jsonbin.io/.. 上保存 json。它看起来像这样:
BIN ID: 6069e05f8be464182c5881f0
{
"status": 200,"data": {
"method": "get","protocol": "https:","hostname": "my-json-server.typicode.com","pathname": "/lalith1403/jsonemaillist/list"
}
}
BIN ID 是我需要的唯一 ID,以便仅显示所需的 jason 数据。我在响应元数据中看到了它,但我无法找到获取它的方法。
这是保存json后的响应:
{"record":{"status":200,"errors":{},"data":{"method":"","protocol":"https:","hostname":"my-json-server.typicode.com","pathname":"/lalith1403/jsonemaillist/list"}},"Metadata":{"id":"6069e6a07d0d5e1833cee3f9","createdAt":"2021-04-04T16:17:36.929Z","private":true}}
然后我的想法是使用附加到 url 的 Id 进行新调用以获得唯一数据,但我可能是错误的..
感谢您的支持
解决方法
根据他们的 documentation,您绝对可以做到这一点!作为读取请求 URL 的一部分,您只需要对以下 URL 执行 GET 请求:https://api.jsonbin.io/v3/b/
可能看起来像这样:
let id = "assigned id here"
let req = new XMLHttpRequest();
req.onreadystatechange = () => {
if (req.readyState == XMLHttpRequest.DONE) {
console.log(req.responseText);
console.log(req);
}
};
req.open("GET","https://api.jsonbin.io/v3/b/" + id + "/latest",true);
req.setRequestHeader("Content-Type","application/json");
req.setRequestHeader("X-Master-Key","myapikey");
req.send(response);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。