提问者:小点点

使用fetch API发生意外的输入结束错误


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'));

console.log

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

我尝试在cros平台上使用fetch the json,为什么Google developer工具中的请求头会显示provision请求头。预览部分可以向我显示json响应,但是响应promise之后的promise无法读取数据,错误发生在上面的控制台日志中。我获取的URL是所有人都可以使用的开源URL。

非常感谢您的帮助。


共1个答案

匿名用户

您说的是模式:'no-cors',因此fetch不会尝试跨源读取数据(默认情况下禁止),并且不会抛出错误(因为您告诉它您不想尝试跨源读取数据)。

将其更改为模式:“cors”

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

另请参见关于同一原产地政策的回答。