【问题标题】:using of fetch API the unexpected end of input error occurr使用 fetch API 意外结束输入错误发生
【发布时间】:2018-05-07 11:53:36
【问题描述】:
fetch('http://www.freegamesforyourwebsite.com/feeds/games/?
tag=platform&limit=100&format=json',
{
method:'GET',
mode:'no-cors',
dataType: 'json',
    headers: {
        'Accept': 'application/json'
    }  

}).then(response =>
response.json()).then(addImage).catch(e => requestError(e,'image'));

控制台日志

SyntaxError: Unexpected end of input
 at fetch.then.response ((index):159)
 at <anonymous>

我尝试在 cros 平台上使用 fetch json,为什么 Google 开发者工具中的请求标头显示我提供的请求标头。并且预览部分可以显示我的 json 响应,但是响应承诺之后的承诺无法读取数据和错误发生在上面的控制台日志中。我获取的URL是开源的,每个人都可以使用。

感谢任何帮助。

【问题讨论】:

    标签: javascript json fetch-api


    【解决方案1】:

    你说mode:'no-cors',所以fetch 不会尝试跨源读取数据(默认情况下是禁止的),它不会抛出错误(因为你告诉它你不想尝试跨来源读取数据)。

    将其更改为mode: "cors"

    确保您请求数据的服务器授予您使用Access-Control-Allow-Origin 标头读取响应的权限。


    另见this answer about the Same Origin Policy

    【讨论】:

      猜你喜欢
      • 2017-11-23
      • 2022-11-11
      • 2018-10-03
      • 2018-01-23
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      相关资源
      最近更新 更多