有人可以看看下面的代码帮我弄清楚我做错了什么吗?我收到此错误JavaScript HTTP请求失败
错误的XMLHttpRequest {readyState的:1,超时:0,withCredentials: 假,上传:XMLHttpRequestUpload,responseURL: “” ...}
当我试图让请求NASA图像galary获取一个图像..
HTML:
<img id="map" src="" alt="image from NASA">
JS:
var get = function(url){
return new Promise(function(resolve,reject){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status == 200){
var result = xhr.responseText;
result = JSON.parse(result);
resolve(result);
}else {
reject(xhr);
}
}
xhr.open("GET",url,true);
xhr.send(null);
})
}
get('https://api.nasa.gov/planetary/apod?api_key=NNKOjkoul8n1CH18TWA9gwngW1s1SmjESPjNoUFo'
)
.then(function(response){
console.log("success",response);
document.getElementById('map').src = response.url;
})
.catch(function(err){
console.log('error',err)
})
HTML模板是 –