Angular 6-错误:注入JSONP的脚本未调用回调

时间:2018-12-20 02:09:36

标签: angular

对于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"

2 个答案:

答案 0 :(得分:0)

尝试一下

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

}

答案 1 :(得分:0)

我只是重新格式化答案

 getSellers(): Observable<Seller[]> {
   return this.http.jsonp(this.apiRoot + '/sellers', "callback")
          .pipe(
            map(res => { 
              reutrn res.results.map(item => {
                  return new Seller( 
                    item.name, 
                    item.id 
                    ); 
                });
            })

            tap(_ => console.log('fetched sellers')),
            catchError(this.handleError('getSellers', []))
          );