从在线托管的 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//latest(您可能错过了 URL 的“最新”结尾,它指定您想要哪个版本的 BIN_ID - 第一个版本、更新后的第二个版本、第三个等等。最新版本只会获得最新版本,我认为这就是您想要的。

可能看起来像这样:

                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);