【发布时间】:2015-08-26 16:43:22
【问题描述】:
我很难从 GULP 本地主机上的 tumblr api 获得良好的响应。
使用邮递员,我从请求 URL 得到正确的响应:
http://api.tumblr.com/v2/blog/{any_blog}/posts?limit=5&api_key={key}
不过,我似乎无法在我的 aurelia 模块中得到响应。我不断收到错误
Fetch API 无法加载http://api.tumblr.com/............ 请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost' 因此不允许访问。
代码如下:
import {inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
import 'fetch';
@inject(HttpClient)
export class Users{
heading = 'Blog Posts';
posts = [];
constructor(http){
http.configure(config => {
config
.withBaseUrl('http://api.tumblr.com/v2/')
.withDefaults({
headers: {
'Access-Control-Allow-Origin': '*'
}
});
});
this.http = http;
}
activate(){
return this.http.fetch('blog/{blog_name}.tumblr.com/posts', { limit: 5, api_key: {api_key} })
.then(response => response.json())
.then(posts => this.posts = posts);
}
}
【问题讨论】:
-
要绕过 Access-Control-Allow-Origin 问题,您需要使用JSONP,即the Tumblr v2 API supports。
-
感谢您的链接,非常有见地
标签: javascript ecmascript-6 aurelia