using of fetch API the unexpected end of input error occurr

痴心易碎 提交于 2019-12-04 05:50:06

问题


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>

I try using the fetch the json with cros platform, why the request header in Google developer tool shows me the provision request header.And the preview part can show me the json response, but the promise after the response promise cannot read the data and the error occurs as shown in the console log above.and the URL I fetch is the open source that can use by everyone.

any help is appreciated.


回答1:


You said mode:'no-cors', so fetch doesn't attempt to read data across origins (which is forbidden by default), and it doesn't throw an error (because you told it you didn't want to try to read data across origins).

Change it to mode: "cors".

Ensure the server you are requesting the data from grants you permission to read the response using the Access-Control-Allow-Origin header.


See also this answer about the Same Origin Policy.



来源:https://stackoverflow.com/questions/47456786/using-of-fetch-api-the-unexpected-end-of-input-error-occurr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!