【发布时间】:2021-08-02 18:20:43
【问题描述】:
我没有通过请求此 API 在控制台中获取 json 数据:https://amlocatapi.us-south.cf.appdomain.cloud/location 使用此标头 { 方法:'GET', 标题:{ '授权':'1q2w3e4r5t6yu7i8', '内容类型':'应用程序/json' } }
<html>
<head></head>
<body>
Hello
</body>
<script>
fetch('https://amlocatapi.us-south.cf.appdomain.cloud/location',
{
method:'GET',
headers:{
'Authorization':'1q2w3e4r5t6yu7i8',
'Content-Type':'application/json'
}
}
).then(response => response.json())
.then(json => console.log(json))
.catch(err => console.log(err));
</script>
</html>
【问题讨论】:
-
如果您检查开发工具控制台,您会看到您的错误:CORS 策略已阻止从源“null”获取访问以及输入意外结束。实际上,您记录的那个空对象不是您的结果,而是
console.log(err)。 -
这能回答你的问题吗? fetch() unexpected end of input
-
它没有解决我的问题。如果我从请求中删除授权标头,那么它将在响应中显示未授权
-
好吧
no-corsallows only limited set of headers in the request 和Authorization不是其中之一。基本上这是您尝试从中获取数据的服务器问题(它没有Access-Control-Allow-Origin标头)和您的浏览器问题,它不允许没有该标头的资源。您可以check this answer了解更多信息。 -
你不只是得到一个CORS错误吗?
Access to fetch at 'https://amlocatapi.us-south.cf.appdomain.cloud/location' from origin '...' has been blocked by CORS policy... 之类的东西,因为它在使用 axios 的 NodeJs 上运行良好
标签: javascript api fetch postman request-headers