Javascript 请求失败,状态代码404错误
我正在尝试构建一个Spotify web应用程序。我想在用户从搜索结果中单击艺术家时显示艺术家的相册。当我尝试下面的代码时,我得到的请求失败,状态代码404 SingerBox.jsJavascript 请求失败,状态代码404错误,javascript,node.js,spotify,spotify-app,Javascript,Node.js,Spotify,Spotify App,我正在尝试构建一个Spotify web应用程序。我想在用户从搜索结果中单击艺术家时显示艺术家的相册。当我尝试下面的代码时,我得到的请求失败,状态代码404 SingerBox.js import React, { Component } from "react"; import Modal from "react-bootstrap/Modal"; import ImageNotFound from "../../ImageNotFound.jpg"; import "../../App.cs
import React, { Component } from "react";
import Modal from "react-bootstrap/Modal";
import ImageNotFound from "../../ImageNotFound.jpg";
import "../../App.css";
import { Link } from "react-router-dom";
import Albums from "../Albums/Albums";
import axios from "axios";
const SingerBox = (props) => {
const { images, name, id } = props;
//check if the image array is empty since some artists' image data provided by the API call are empty
const singer_img = images.length === 0 ? ImageNotFound : images[0].url;
const handleClick = () => {
axios
.get(`http://localhost:4000/${id}`, {
params: {
id: id,
},
})
.then((res) => {
console.log(`Returned album data from the server: ${res}`);
return <Albums albums={res.data} />;
})
.catch((err) => console.log(err));
};
return (
<>
<Link to={`/albums/${id}`}>
<div className="box" onClick={() => handleClick()}>
<div>
<img className="singer-img" src={singer_img} alt="Card image" />
{name}
</div>
</div>
</Link>
</>
);
};
export default SingerBox;
我还想知道如何改进代码的结构,比如axios调用。它们看起来很凌乱,但我不知道从哪里开始修复它们。在您试图使用的SingerBox组件中,${id}在您的server.js文件中没有这样的API。这就是为什么您会遇到404错误,这意味着找不到
const handleClick = () => {
axios
.get(`http://localhost:4000/${id}`, {
params: {
id: id,
},
})
.then((res) => {
console.log(`Returned album data from the server: ${res}`);
return <Albums albums={res.data} />;
})
.catch((err) => console.log(err));
};
在SingerBox组件中,您试图使get on${id}在server.js文件中没有这样的API。这就是为什么您会遇到404错误,这意味着找不到
const handleClick = () => {
axios
.get(`http://localhost:4000/${id}`, {
params: {
id: id,
},
})
.then((res) => {
console.log(`Returned album data from the server: ${res}`);
return <Albums albums={res.data} />;
})
.catch((err) => console.log(err));
};
如果您从浏览器向Spotify发出GET请求,是否能够获取响应?如果您从浏览器向Spotify发出GET请求,是否能够获取响应?
const handleClick = () => {
axios
.get(`http://localhost:4000/${id}`, {
params: {
id: id,
},
})
.then((res) => {
console.log(`Returned album data from the server: ${res}`);
return <Albums albums={res.data} />;
})
.catch((err) => console.log(err));
};
app.get("/:id", (req, res) => {
" your API logic"
});