首页 文章

Angular 5(Ionic)JSONP请求

提问于
浏览
0

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

这是我做的:

  • app.module.ts

import { HttpClientModule, HttpClientJsonpModule } from '@angular/common/http';
...
导入:[BrowserModule,IonicModule.forRoot(MyApp) , HttpClientModule, HttpClientJsonpModule ],
...

  • 在组件的 .ts 文件中:
    import from 'rxjs/Observable'; import from '@angular/common/http';
    ...

//在课堂里
films: Observable< any >; constructor(public navCtrl: NavController, public navParams: NavParams, public http: HttpClient) { const url = "https://api.themoviedb.org/3/movie/550?api_key= [MY_API_KEY]&callback=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

    您正在使用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中 .

相关问题