我正在与美国宇航局的每日天文图片合作,我已经设法提取了图片的描述,图片的标题,以及谁拍摄了这张照片。我唯一缺少的就是中心部分本身,来自美国宇航局的实际图片。我已经能够让其他图像加载很好,但这个项目主要是图片,他们似乎没有一个想为我工作。当查看我的控制台时,我一直收到一个404错误,显示为"GET https://apod.nasa.gov/apod/image/2004/WindmillStarTrails1024.jpgstyle='width:100%'/ 400 (Bad Request)“
我认为这是一个错误的获取网址的图片,但我不确定如何解决它。我已经成功地得到了其他图片的工作,并以完全相同的方式格式化了这一张,所以我不确定是什么让这张图片与其他图片不同。下面是包含我使用的脚本的HTML。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> NASA API Demo </title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div id="output">
<thing id="img"></thing>
<p id = "copyright"> </p>
<h1 id = "title"></h1>
<p id="explanation"> </p>
</div>
<script>
fetch('https://api.nasa.gov/planetary/apod?api_key=(My Key Here)
.then((response) => {
return response.json();
})
.then((endUpWith) => {
document.getElementById("img").innerHTML="<img src=" +endUpWith.url+ "style='width:100%'/>";
document.getElementById("copyright").innerHTML="By " + endUpWith.copyright;
document.getElementById("title").innerHTML=endUpWith.title;
document.getElementById("explanation").innerHTML=endUpWith.explanation;
});
</script>
</body>
</html>发布于 2020-04-18 02:51:59
检查添加到页面的元素。它看起来就像
<img src=https://apod.nasa.gov/apod/image/2004/WindmillStarTrails1024.jpgstyle='width:100%' />因为您没有使用引号,并且url与样式属性挂钩。在源代码周围添加引号,添加空格。
"<img src='" +endUpWith.url + "' style='width:100%'/>"
^ ^^发布于 2020-04-18 02:57:49
我会将双引号转换为单引号,至少对于这一行是这样的:
document.getElementById("img").innerHTML='<img src="' +endUpWith.url+ '" style="width:100%"/>';更容易阅读。实际上,我建议使用单引号来处理JS字符串。
https://stackoverflow.com/questions/61278639
复制相似问题