我正在使用AWS网关,并得到以下错误:
Uncaught TypeError:未能获取
我的API只是返回一个简单的JSON字符串。通过AWS控制台,我可以通过curl、Postman和测试获得一个成功的响应。但是,使用JavaScript,我得到了一个错误。我怀疑AWS需要一个特定的头。
我把代码和原来的网站分开了。一些不必要的部件已被移除。
<script type = "text/javascript">
const GET_url = '<url>'
const POST_url = '<url>'
async function getInvokeFromAPI() {
const response = await fetch(GET_url, {
method: 'GET'
});
const data = await response.json();
}
async function postInvokeFromAPI() {
const respone = await fetch(POST_url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
});
const data = await response.json();
}
</script>
发布于 2022-04-01 11:51:00
当发生错误时,查看控制台输出将是有帮助的。
但是,如果它与Postman一起工作,而不是在fetch API中工作,那么您可能有一个CORS错误。
您可以查看MDN的此页以获得有关CORS的更多信息,以防出现错误:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS。
如果是您的错误,它将被记录在开发工具中的浏览器控制台中。
https://stackoverflow.com/questions/71711519
复制