所以我试图从使用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;
答案 0 :(得分:-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);
}
&#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;