Error retrieving jsonp from aurelia http client

喜夏-厌秋 提交于 2019-12-08 02:16:53

问题


I have an application I am creating using Aurelia. I wish to retrieve the latest images from a user in instagram via ajax. I tried using the Aurelia http client because that's what the Flickr example on Aurelia's getting started page uses.

If I use http.get, then I get a CORS error from instagram. Trying http.jsonp, the inspected response shows the correct format, but I get what looks like a parsing error (Unexpected token ":") before the callback that handles the response is being executed.

I am able to successfully retrieve the images via AJAX using jQuery, but I am curious to know what I am doing wrong with the Aurelia http client.

Here are the relevant bits of the view model:

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
            };
        });
    });
  }
}

回答1:


Instagram API expects callback GET parameter to be present in request URL. This parameter should be a function name to wrap response object into (Aurelia will add something like &callback=jsonp_callback_59563). The reason why it works with jQuery.jsonp is that it adds callback parameter automatically behind the scene.

Correct code then should be:

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
        };
    });
});


来源:https://stackoverflow.com/questions/31793604/error-retrieving-jsonp-from-aurelia-http-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!