首页 > 解决方案 > Angular 6 - 错误:JSONP 注入脚本未调用回调

问题描述

对于 jsonp 请求,我收到此错误:

Error: JSONP injected script did not invoke callback.

不知道如何使用回调。

  getSellers(): Observable<Seller[]> {
    return this.http.jsonp<Seller[]>(this.apiRoot + '/sellers', 'callback')
      .pipe(
        tap(_ => console.log('fetched sellers')),
        catchError(this.handleError('getSellers', []))
      );
  }

错误:

HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "JSONP Error", url: "http://localhost:4300/api/sellers?__ng_jsonp__.__req$%7Bthis.times%7D.finished=ng_jsonp_callback_0", ok: false, …}
error: Error: JSONP injected script did not invoke callback. at HTMLScriptElement.onLoad (http://localhost:4200/vendor.js:7224:32) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2743:31) at Object.onInvokeTask (http://localhost:4200/vendor.js:36916:33) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2742:36) at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost:4200/polyfills.js:2510:47) at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:2818:34) at invokeTask (http://localhost:4200/polyfills.js:3862:14) at HTMLScriptElement.globalZoneAwareCallback (http://localhost:4200/polyfills.js:3888:17)
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
message: "Http failure response for http://localhost:4300/api/sellers?__ng_jsonp__.__req$%7Bthis.times%7D.finished=ng_jsonp_callback_0: 0 JSONP Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "JSONP Error"
url: "http://localhost:4300/api/sellers?__ng_jsonp__.__req$%7Bthis.times%7D.finished=ng_jsonp_callback_0"

标签: angular

解决方案


尝试这个

getSellers(): Observable<Seller[]> {
return this.http.get(this.apiRoot + '/sellers')
  .pipe(
    tap(_ => console.log('fetched sellers')),
    catchError(this.handleError('getSellers', []))
  );

}


推荐阅读