如果出现错误,我对获取API有一些疑问 [英] I have some doubts regarding the fetch api in case of errors

查看:50
本文介绍了如果出现错误,我对获取API有一些疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试获取API,但尚不清楚何时将其拒绝.例如,当我尝试以下代码:fetch("https://stackoverflow.com/")时,它会被拒绝,而如果我插入一个完全随机的文本,例如"aoishoaisoab",则会被拒绝.即使该URL不存在也可以解决.我已经读到它只有在出现网络错误时才会被拒绝.那怎么会被拒绝呢?

I have been experimenting with the fetch api, and it's still not clear when it is rejected. For example when I try the following: fetch("https://stackoverflow.com/") it is rejected whereas if i insert a completely random text like "aoishoaisoab" it is resolved eventhough such URL doesnt exists. I have read that it is only rejected if there is a network error. Then how come it is rejected?

推荐答案

请确保您没有从浏览器发出此请求,您将得到一个

Be sure that you are not making this request from your browser, you will get a CORS error since the fetched page is not hosted on the same domain as the JS Code.

出于测试目的,您可以使用

For testing purposes you can use a CORS plugin that allows requests like that.

这篇关于如果出现错误,我对获取API有一些疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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