-3

So i'm trying to get specific string from api that uses json. Heres what im trying to do: HERES THE IDEA IM TRYING TO DO : https://us.mc-api.net/example/uuid So i want the mayed505 replaced by the name in the text box and submits when the button is clicked ( this is minecraft api which you replace PLAYERNAME and put someones name and all the information will appear.) 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">
  • Is your questions how to get data from a field? [That question exists many times over already.](http://stackoverflow.com/questions/11563638/javascript-get-input-text-value) – Matthew Herbst Mar 29 '16 at 06:03
  • type="hidden name"? store the url in variable. on click of the button create the url with the username and make the request var username = document.getElementById('playerName').value; var url = 'https://us.mc-api.net/v3/uuid/'+username; getJSON(url).then(function(data) {................. – a45b Mar 29 '16 at 06:05
  • @MatthewHerbst yes. and replace mayed505 with the name in text input – MayedGamer Mar 29 '16 at 06:15
  • @iamkdev can u explain more? i cant see the code. – MayedGamer Mar 29 '16 at 06:16
  • @iamkdev here i tried doesn't work either can u make me a jsfiddle? https://jsfiddle.net/m600yt3a/ and heres the api im trying to use https://us.mc-api.net/example/uuid – MayedGamer Mar 29 '16 at 06:22
  • I change few thing like the Check my answer if thats works for you – a45b Mar 29 '16 at 06:26

1 Answers1

-2

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);
}
<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()">
a45b
  • 489
  • 3
  • 19
  • Check the console for respond I got this {"full_uuid":"ab77ddd0-898b-453c-8378-5cfdd8dcb51f","source":"cache","query":"mayed505","name":"Mayed505","took":0,"uuid":"ab77ddd0898b453c83785cfdd8dcb51f"} – a45b Mar 29 '16 at 06:28
  • I checked console it says: (index):92 Uncaught ReferenceError: fetchData is not defined – MayedGamer Mar 29 '16 at 06:28
  • Man you have to learn basics .... What you are accepting if you have done nothing with the respond. Check the respond for the request – a45b Mar 29 '16 at 06:31
  • Well the problem im 13...... im simple app maker https://play.google.com/store/apps/details?id=com.mayed.minecraftskinsearch i made this app and im trying to improve it since i dont know json verywell – MayedGamer Mar 29 '16 at 06:32
  • is it possible? to get full_uuid like my old code? everything works fine i guess i have to learn json – MayedGamer Mar 29 '16 at 06:38
  • In your old code you have not done anything to the button onClick or onSubmit event. And also the url need the username to get the data. So, you have to make that before getJSON execute. – a45b Mar 29 '16 at 07:05