Wikipedia API的fetch()导致“尝试获取资源时出现TypeError:NetworkError". [英] fetch() with the Wikipedia API results in "TypeError: NetworkError when attempting to fetch resource."

查看:180
本文介绍了Wikipedia API的fetch()导致“尝试获取资源时出现TypeError:NetworkError".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fetch('https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json')
    .then(  
        function(response) {  
            if (response.status !== 200) {  
                console.log('Looks like there was a problem. Status Code: ' + response.status);  
                return;  
            }

            // Examine the text in the response  
            response.json().then(function(data) {  
                console.log(data);  
            });  
        }  
    )
    .catch(function(err) {  
        document.write('Fetch Error :-S', err);  
    });

以下列出了我正在使用的提取地址: https://www.mediawiki.org /wiki/API:Main_page 下的简单示例.目前,它使用TypeError: NetworkError when attempting to fetch resource.捕获了底部的错误,到目前为止,在尝试使用fetch()进行其他操作后,我无法从API访问任何数据

The fetch address I'm using is listed here: https://www.mediawiki.org/wiki/API:Main_page under simple sample. Currently it catches on the error on the bottom with TypeError: NetworkError when attempting to fetch resource. So far I've been unable to access any data from the API after trying different things with fetch()

任何帮助将不胜感激!

该项目位于Codepen上: http://codepen.io/javascriptisscary/pen/RazKWB

The project is on codepen here: http://codepen.io/javascriptisscary/pen/RazKWB

推荐答案

问题是由于fetch()使用CORS引起的.但是,当我更改为mode: "no-cors"时,不再受到跨域请求的阻止,而是获得状态码0.

The problem is due to fetch() using CORS. However, when I changed to mode: "no-cors" I was no longer blocked on Cross-origin request but given a status code of 0.

根据Google的本文档: https://developers.google.com/web/updates/2015/03/introduction-to-fetch?hl=zh_CN

According to this documentation from google: https://developers.google.com/web/updates/2015/03/introduction-to-fetch?hl=en

'no-cors'旨在向没有CORS标头的其他来源发出请求,并导致响应不透明,但如上所述,目前在窗口全局范围中这是不可能的."

" 'no-cors' is intended to make requests to other origins that do not have CORS headers and result in an opaque response, but as stated, this isn’t possible in the window global scope at the moment."

因此,当前要访问Wikipedia api,将不得不使用与fetch()

So currently to access the wikipedia api, one will have to use a different way than fetch()

这篇关于Wikipedia API的fetch()导致“尝试获取资源时出现TypeError:NetworkError".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆