如果出现错误,我对获取API有一些疑问 [英] I have some doubts regarding the fetch api in case of errors
问题描述
我一直在尝试获取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屋!