如何修复net::ERR_ABORTED 404(未找到)时,调用api路由使用next/链接在NextJS



我已经创建了一个api路由来下载CSV文件,我在使用下一个链接时得到这个错误。我不能直接切换到,因为我的应用程序无法构建,所以我无能为力。组件是简单的

const DownloadCsvPage = () => {
return (
<section className="w-full h-screen grid place-content-center">
<Link href="/api/db-to-csv">
<a className="px-20 py-9 border text-black no-underline">
Download CSV
</a>
</Link>
</section>
);
};

我试图遵循这篇文章https://www.loudnoises.us/solving-next-static-pages-js-404-not-found/,但设置href为当前页面路径,至于api路由的路径,只将api路由的url附加到本地主机,但没有触发下载。像

<Link href="/download" as="/api/db-to-csv">
<a className="px-20 py-9 border text-black no-underline">
Download CSV
</a>
</Link>

阅读此解决方案https://exerror.com/js-file-gets-a-neterr_aborted-404-not-found/后,我认为移动文件我想下载到公共文件夹会做的工作,但它没有。由于

如果你想调用API,最好使用正常的div标签,并添加onClick事件处理程序,并附加一个函数,该函数将在fetch或axios的帮助下调用API,在该函数中,你可以使用。then(result)和。catch(error)方法来处理相应的API响应。