javascript - 如何使用 fetch 检索 JSONP?
问题描述
我无法从此公共端点加载数据:
http://api.flickr.com/services/feeds/photos_public.gne?format=json
我收到以下错误:
Access to fetch at 'http://api.flickr.com/services/feeds/photos_public.gne?format=json' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
这个怎么做?
解决方案
Jsonp 不是这样工作的,因为它旨在解决同源策略。如果 flickr API 支持 CORS,则可以将其与 fetch 一起使用,但如果要使用 jsonp,则需要添加脚本标签和回调。进一步阅读:https ://en.wikipedia.org/wiki/JSONP
例子:
<script type="application/javascript">
window.jsonFlickrFeed = function(response) {
alert(response);
};
</script>
<script type="application/javascript" src="http://api.flickr.com/services/feeds/photos_public.gne?format=json">
</script>
推荐阅读
- java - 'batchDataSourceInitializer' 和 'entityManagerFactory' 之间的循环依赖关系
- java - Java如何将用户输入保存在数组中
- pyspark - 根据具有多个交货日期的客户 ID 对总订单进行求和
- oracle - 在Oracle中出现单个逗号时拆分字符串,当出现多个逗号时不拆分
- websphere - 如何在 IBM Watson Chatbot 中添加下拉菜单
- python - AttributeError: 'super' object has no attribute – 通过多层继承调用基类方法
- c - C 输出不符合预期 - 全局变量返回不同的值
- sql - 用于将子行组合成单行视图的 SQL 查询
- google-cloud-platform - 从同一内部网络中的 pod 连接到邮件服务器(imap)时,gke '连接被拒绝'
- python-2.7 - 如何最好地模拟python中的简单随机延迟