如果我使用Axios或Fetch向此公共端点发出Ajax请求:
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: 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.
但是,如果我使用Postman发出相同的请求,我可以看到结果很好。该如何解决?
答案 0 :(得分:1)
这不是邮递员与axios或提取的问题。问题是服务器返回的是jsonp而不是json。 Axios和Fetch都不支持jsonp。
为什么不在这里使用ajax?
$(document).ready(function() {
$.ajax({
url: 'http://api.flickr.com/services/feeds/photos_public.gne?format=json',
dataType: 'jsonp',
jsonpCallback: 'jsonFlickrFeed'
});
window.jsonFlickrFeed = function(response) {
console.log(response)
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>