如何使JSON从输入文本获取并找到它? (在主题中解释)

时间:2016-03-29 05:55:09

标签: html json

所以我试图从使用json的api获取特定字符串。 继承人试图做的事情: 以下是想要做的想法:https://us.mc-api.net/example/uuid 所以我希望mayed505替换为文本框中的名称,并在单击按钮时提交(这是我的替换PLAYERNAME的Minecraft api,并且会显示某些名称,所有信息都会出现。)https://us.mc-api.net/v3/uuid/PLAYERNAME



var getJSON = function(url) {
  return new Promise(function(resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open('get', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() {
      var status = xhr.status;
      if (status == 200) {
        resolve(xhr.response);
      } else {
        reject(status);
      }
    };
    xhr.send();
  });
};

getJSON('https://us.mc-api.net/v3/uuid/mayed505').then(function(data) {

//                                     ^^^^^^^^ I want that be replaced with the input text in HTML like:
  //https://us.mc-api.net/v3/uuid/INPUT FROM TXT BAR
     

    //alert('Your Json result is:  ' + data.full_uuid); //you can comment this, i used it to debug

    full_uuid.innerText = data.full_uuid; //display the result in an HTML element
    }, function(status) { //error detection....
  alert('Something went wrong.');
});

<div id="full_uuid" style="color:red"></div>
<input id="playerName" type="hidden name" value="Username">
<input data-inline="true" class="btn btn-primary" type="button" value="Submit to get UUID">
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:-2)

&#13;
&#13;
var getJSON = function(url) {
  return new Promise(function(resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open('get', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() {
      var status = xhr.status;
      if (status == 200) {
        resolve(xhr.response);
        alert(JSON.stringify(xhr.response));
      } else {
        reject(status);
      }
    };
    xhr.send();
  });
};

getJSON(url).then(function(data) {

//                                     ^^^^^^^^ I want that be replaced with the input text in HTML like:
  //https://us.mc-api.net/v3/uuid/INPUT FROM TXT BAR
     

    //alert('Your Json result is:  ' + data.full_uuid); //you can comment this, i used it to debug

    full_uuid.innerText = data.full_uuid; //display the result in an HTML element
    }, function(status) { //error detection....
  alert('Something went wrong.');
});

function fetchData(){
  var username = document.getElementById('playerName').value; 
  var url = 'https://us.mc-api.net/v3/uuid/'+username; 
  getJSON(url);
}
&#13;
<div id="full_uuid" style="color:red"></div>
<input id="playerName" type="text" name="username" value="mayed505">
<input data-inline="true" type="button" class="btn btn-primary" type="button" value="Submit to get UUID" onclick="fetchData()">
&#13;
&#13;
&#13;