我正在尝试构建一个页面,以在Marvel API中返回请求的结果。
我直接在var name中使用'nameStartsWith'的值;但用户必须在document.getElementById('name')。value;
中输入 document.addEventListener("DOMContentLoaded", bindButtons);
var publicAPI_Key = "6ab90950313433e5bd781aa4db43aea6";
var privateAPI_key = "3ef90f3ccdddec42b24c90dc0eea83849d58d785";
function bindButtons(event){
var number = "thesoer";
var req = new XMLHttpRequest();
var url = "http://gateway.marvel.com/v1/public/characters?nameStartsWith=";
var name = "Iron";
//var name = document.getElementById('name').value;
console.log("name= " + name);
var query = url + name+ "&limit=5&ts=" + number + "&apikey=" + publicAPI_Key + "&hash=5dd9a3d1a63da369a40509a508f2fd20";
console.log("query= " + query);
document.getElementById("submit").addEventListener('click', function(event){
//document.getElementById("name").textContent = "";
});
req.open('GET', query, true);
req.setRequestHeader('Content-Type', 'application/json');
req.addEventListener('load', function(){
if(req.status >= 200 && req.status < 400){
var result = JSON.parse(req.responseText);
for(var i = 0; i < result.data.count; i++){
document.getElementById("char").innerHTML += "<br>" + result.data.results[i].name;
console.log(i);
console.log(result.data.results[i].name);
}
}
event.preventDefault();
});
req.send(null);
}
现在代码返回5个结果,名称为iron,但没有用户输入;