WoT抽认卡连接到MyJSON

时间:2019-02-03 23:54:33

标签: json

我目前正在开发一个娱乐应用程序,它将使我能够从api中返回与WoT书系列中与给定输入相匹配的字符信息。我正在测试,以确保我仍然知道如何编写api,但显然不知道。我已经尝试模仿其他api(pokemon api)的语法,但是我仍然无法真正编写它,因此输入触发内容传递到我的页面。

我在api.MyJson.com上编写了以下api,它已经接受了输入。我希望用户输入现在是ID(例如1、2等),并且当我能够返回“未定义”时,因此它不是js innerhtml行。 我还留下了一点console.logs,它们似乎在连接过程中一直被触发,甚至已经使用精确的代码(甚至文件)在其他api上成功进行了测试

API语法为:

 {
   "characters": [
     {
       "id": 1,
       "gender": "male",
       "name": "Rand al'Thor",
       "archtype": "Taveren",
          "bio": "Rand al'Thor born in the year 978 NE on the slopes of"
      },
     {
  "id": 2,
  "gender": "male",
  "name": "Perrin Aybara",
  "archetype": "Taveren",
  "bio": " He is a strong ta'veren, a wolfbrother, and has exceptional skills in manipulating Tel'aran'rhiod. He is also husband to the Lady Faile ni Bashere t'Aybara, first in succession to the vacant throne of Saldaea. "
},
{
  "id": 3,
  "gender": "male",
  "name": "Matrim Cauthon",
  "archetype": "Taveren",
  "bio": " Matrim Cauthon, born in the year 978 NE and usually called 'Mat', he is one of the main characters of the series. He is the Marshal-General of the Band of the Red Hand, and the husband of the Empress of the Seanchan empire, as well as one of the greatest generals the world has ever seen. "
}
     ]
 }     

当我搜索api链接https://api.myjson.com/bins/zjg2k/时,找不到数据,但是当我在链接末尾包含任何ID时(例如https://api.myjson.com/bins/zjg2k/2),它可以不能达到(与《 Pokemon api》相反)。

但是,我可以注意到,我有一个console.log确实会重复,在'onreadystatechange = function()命令下,我只希望看到它一次。 这是实际的代码:

    window.onload = function(){
document.getElementById("swSubmit").addEventListener('click', getSW);
 } 

  function getSW(){
        console.log("get SW! ");
    let swId = document.getElementById('swId').value;
    let xhttp = new XMLHttpRequest();

     xhttp.onreadystatechange = function(){
            console.log("ready state is changing!! CHAAAANGINNNNGG!!");

            if(xhttp.readyState==4 && xhttp.status==200){
                let sw = JSON.parse(xhttp.responseText);
                setValues(sw); 
            }

        }
        xhttp.open("GET",'https://api.myjson.com/bins/6fcl8' + swId);

            console.log("getting here?"); 
        xhttp.send();
 }
  function setValues(sw){
    document.getElementById("WoTName").innerHTML = sw.name;
    // document.getElementById("WoT").innerHTML = sw.gender;


  } 

tbh,我是比编码器更大的WoT迷,并且我对此有一个漂亮的前端页面,我真的很想在亚马逊Prime系列问世之前完成这项工作!但是我需要一些基本的帮助来使它具有可扩展性!谢谢!

0 个答案:

没有答案