Angular 5(Ionic)JSONP请求

时间:2018-01-22 10:54:45

标签: angular jsonp ionic3

我想连接到http://api.themoviedb.org以执行GET请求。由于我使用浏览器(ionic serve -l),我收到CORS错误。为了避免CORS错误,我尝试使用JSONP但没有成功。

这是我做的:

  • app.module.ts

    导入{HttpClientModule,HttpClientJsonpModule}来自 '@角/普通/ HTTP';
    ...
    进口:[   BrowserModule,   IonicModule.forRoot(MyApp的)的,   HttpClientModule,   HttpClientJsonpModule ],
    ...

  • 在组件的 .ts 文件中:
    从'rxjs / Observable'导入{Observable};
    从'@ angular / common / http'中导入{HttpClient};

    ......

    //在班上 电影:可观察的<任何&gt ;;
    构造函数(public navCtrl:NavController,public navParams:NavParams, public http:HttpClient)
    {
     const url =“https://api.themoviedb.org/3/movie/550?api_key= [MY_API_KEY]&安培;回调= JSONP_CALLBACK“;
        this.films = this.http.jsonp(url,'callback');
    }

    ......

  • 在组件的 .html 中:

      <ion-content padding>
          <ion-list>
            <button ion-item>
              {{(films | async)?.title}}
            </button>
          </ion-list>
        </ion-content>
    

我收到错误,告知它无法解析响应:

以下是错误([MY_API_KEY]是错误代码中的实际API_KEY):

Uncaught TypeError: ["ng_jsonp_callback_0","JSONP_CALLBACK"] is not a function
    at 550?api_key=[MY_API_KEY]&callback=ng_jsonp_callback_0&callback=JSONP_CALLBACK:1
(anonymous) @ 550?api_key=[MY_API_KEY]&callback=ng_jsonp_callback_0&callback=JSONP_CALLBACK:1
core.js:1350 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "JSONP Error", url: "https://api.themoviedb.org/3/movie/550?api_key=303…lback=ng_jsonp_callback_0&callback=JSONP_CALLBACK", ok: false, …}

以下是收到的答复:

["ng_jsonp_callback_0", "JSONP_CALLBACK"]({"adult":false,"backdrop_path":"/87hTDiay2N2qWyX4Ds7ybXi9h8I.jpg","belongs_to_collection":null,"budget":63000000,"genres":[{"id":18,"name":"Drama"}],"homepage":"http://www.foxmovies.com/movies/fight-club","id":550,"imdb_id":"tt0137523","original_language":"en","original_title":"Fight Club","overview":"A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.","popularity":61.167619,"poster_path":"/adw6Lq9FiC9zjYEpOqfq03ituwp.jpg","production_companies":[{"name":"Twentieth Century Fox Film Corporation","id":306},{"name":"Regency Enterprises","id":508},{"name":"Fox 2000 Pictures","id":711},{"name":"Taurus Film","id":20555},{"name":"Linson Films","id":54050},{"name":"Atman Entertainment","id":54051},{"name":"Knickerbocker Films","id":54052}],"production_countries":[{"iso_3166_1":"DE","name":"Germany"},{"iso_3166_1":"US","name":"United States of America"}],"release_date":"1999-10-15","revenue":100853753,"runtime":139,"spoken_languages":[{"iso_639_1":"en","name":"English"}],"status":"Released","tagline":"Mischief. Mayhem. Soap.","title":"Fight Club","video":false,"vote_average":8.300000000000001,"vote_count":11124})

据我所知,它应该返回一个javascript函数的名称来处理数据。但是我传递回调的方式似乎有些不对,无法找到关于如何使用新的HttpClient进行操作的示例。

1 个答案:

答案 0 :(得分:1)

您正在使用httpClient模块,您应该像这样调用它:

const url = "https://api.themoviedb.org/3/movie/550?api_key= [MY_API_KEY];//no callback param
this.films = this.http.jsonp(url, 'callback');

函数调用会将callback = JSONP_CALLBACK添加到您的URL中。