从http客户端检索json出错
Error retrieving jsonp from aurelia http client
我有一个应用程序,我正在使用Aurelia创建。我希望通过ajax从instagram的用户检索最新的图像。我尝试使用Aurelia http客户端,因为这就是Aurelia入门页面上的Flickr示例所使用的。
如果我使用http.get
,那么我从instagram得到一个CORS错误。尝试http.jsonp
,检查的响应显示了正确的格式,但在执行处理响应的回调之前,我得到了一个解析错误(意外令牌":")。
我am能够使用jQuery通过AJAX成功检索图像,但我很好奇知道我在Aurelia http客户端做错了什么。
以下是视图模型的相关部分:
import {inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-http-client';
@inject(HttpClient)
export class Welcome {
images = [];
url = '[INSTAGRAM_URL]';
constructor(http){
this.http = http;
}
activate(){
return this.http.jsonp(this.url).then(response => {
this.images = response.data.map(function (d) {
return {
src: d.images.standard_resolution.url,
caption: d.caption.text,
when: d.created_time
};
});
});
}
}
Instagram API期望callback
GET参数出现在请求URL中。这个参数应该是一个函数名,以便将响应对象包装到其中(Aurelia将添加类似&callback=jsonp_callback_59563
的东西)。它与jQuery一起工作的原因。jsonp的缺点是它在后台自动添加callback
参数。
return this.http.jsonp(this.url, 'callback').then(response => {
this.images = response.content.data.map(function (d) {
return {
src: d.images.standard_resolution.url,
caption: d.caption.text,
when: d.created_time
};
});
});
相关文章:
- 解析JSON并从中删除对象会出错
- 加载db.json时出错
- 解析将数据从 JSON 发送到 Django 时出错
- 使用 RESTful API,如果未定义特定的响应 json 级别,我如何在客户端不出错
- 从静态文件加载json时出错
- 解析从js发送到Java的json字符串时出错
- JSON.parse在循环对象上未出错
- 将JSON映射到observableArray时出错
- 406向Spring Controller发送JSON时出错
- 在NodeJS中编辑JSON数据时出错
- 尝试从JSON加载单词时出错
- 使用JQuery和JSON将对象发送到JAX-RS时出错
- 在变量中存储 JSON 数据时出错
- 从标记内容解析 JSON 时出错
- 从 JavaScript 中的本地 JSON 文件读取时出错
- 转换为 JSON 的 XML 节点会出错
- Newtonsoft Json.net 解析布尔值时出错
- 405 AJAX 调用 JSON 时出错(xhr.status)
- 从http客户端检索json出错
- 从包含字母数字的命名空间读取JSON出错