【发布时间】:2016-01-30 03:54:18
【问题描述】:
我有这段代码,我正在尝试返回 Flickr API,但是我收到以下错误。
跨域请求被阻止:同源策略不允许读取 远程资源在
http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback={callback}&tags=london&tagmode=any&format=json。 这可以通过将资源移动到同一域或 启用 CORS。
如何在我的代码中启用此功能?
enter
MyFeed.prototype.getFeed = function(data) {
console.log(f.feedUrl);
var request = new XMLHttpRequest();
request.open('GET', f.feedUrl, true);
request.onload = function () {
if (request.status >= 200 && request.status < 400) {
// Success!
console.log(request.responseText);
var data = JSON.parse(request.responseText);
} else {
// We reached our target server, but it returned an error
console.log("error");
}
};
request.onerror = function () {
// There was a connection error of some sort
};
request.send();
}here
【问题讨论】:
-
5 年前我有同样的问题,答案是使用 JSONP api.jquery.com/jquery.getjson 和 jquery。 stackoverflow.com/questions/3112399/…
标签: javascript json flickr