【问题标题】:Uncaught (in promise) SyntaxError: Unexpected end of input after json fetch [duplicate]未捕获(承诺中)SyntaxError:json fetch后输入意外结束[重复]
【发布时间】:2019-01-17 09:16:52
【问题描述】:

当我尝试从 Tibia API 获取 JSON 时,我得到了两件事。

错误:tibia.js:8 Uncaught (in promise) SyntaxError: Unexpected end of input

警告:跨域读取阻塞 (CORB) 阻止了跨域响应 https://api.tibiadata.com/v2/characters/Burdeliusz.json

class Tibia {
    constructor() {}
    async getCharacter(char) {
        const characterResponse =
            await fetch(`https://api.tibiadata.com/v2/characters/${char}.json`, {
                mode: 'no-cors'
            });
        const character = await characterResponse.json();
        return {
            character
        }
    }
}

我搜索了类似的问题,但找不到解决方法。

【问题讨论】:

  • 使用{mode:'no-cors'} 发出的任何获取请求都会导致不透明的回复,这意味着您不能使用res.json().text() 或dot-anything。来自 MDN; 此外,JavaScript 可能不会访问结果响应的任何属性。 developer.mozilla.org/en-US/docs/Web/API/Request/mode
  • 那么,我无法从这个 API 获取数据?
  • 查看我的答案,您可以使用代理访问它。见jsfiddle.net/RouzbehHz/b95vcdhm/2(等一下,tibiadata.com 的响应速度很慢)
  • 是的,您需要使用代理(嘘...但可以)或启动您自己的后端服务器来为您获取数据。如果你只是需要它又快又脏,那就去代理吧。
  • @D.Wasilewski 太棒了!很高兴能帮上忙,编码愉快! :)

标签: javascript promise cors


【解决方案1】:

这是因为端点没有在响应头中传递正确的参数。

标题应包括:

"Access-Control-Allow-Origin" : "*", 
"Access-Control-Allow-Credentials" : true 

我用 Postman 进行了测试,响应有 8 个标头:https://api.tibiadata.com/v2/characters/Burdeliusz.json

Connection →keep-alive
Content-Length →683
Content-Type →application/json; charset=utf-8
Date →Thu, 09 Aug 2018 20:05:30 GMT
Server →nginx/1.10.3
Strict-Transport-Security →max-age=63072000; includeSubdomains; preload
X-Content-Type-Options →nosniff
X-Frame-Options →DENY

访问控制允许来源示例:https://api.spacexdata.com/v2/launches

Access-Control-Allow-Origin →*
CF-RAY →447cd76c595fab66-YYZ
Connection →keep-alive
Content-Encoding →gzip
Content-Type →application/json; charset=utf-8
Date →Thu, 09 Aug 2018 20:06:08 GMT
Expect-CT →max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server →cloudflare
Set-Cookie →__cfduid=d1dce3c5d11de37f960c7b47dc4f7d6701533845168; expires=Fri, 09-Aug-19 20:06:08 GMT; path=/; domain=.spacexdata.com; HttpOnly; Secure
Strict-Transport-Security →max-age=15552000; includeSubDomains
Transfer-Encoding →chunked
Vary →Accept-Encoding, Origin
X-Cache-Status →EXPIRED
X-Content-Type-Options →nosniff
X-DNS-Prefetch-Control →off
X-Download-Options →noopen
X-Frame-Options →SAMEORIGIN
X-Response-Time →151ms
X-XSS-Protection →1; mode=block

您可以尝试让 tibiadata 的人添加标题

使用代理访问端点:http://jsfiddle.net/RouzbehHz/b95vcdhm/2/

var proxyUrl = 'https://cors-anywhere.herokuapp.com/',
    targetUrl = 'https://api.tibiadata.com/v2/characters/Burdeliusz.json'
fetch(proxyUrl + targetUrl)
  .then(blob => blob.json())
  .then(data => {
    console.table(data);
    document.querySelector("pre").innerHTML = JSON.stringify(data, null, 2);
    return data;
  })
  .catch(e => {
    console.log(e);
    return e;
  });

您可以重新创建代理服务器:

git clone https://github.com/Rob--W/cors-anywhere.git
cd cors-anywhere/
npm install
heroku create
git push heroku master

【讨论】:

    猜你喜欢
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 2020-01-08
    • 2023-03-23
    • 2021-01-09
    相关资源
    最近更新 更多