Uncaught ReferenceError: __ng_jsonp____req0_finish

2019-03-02 18:38发布

I have an Angular application and I'm using JSONP as well.

This is my service:

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Page } from './page';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { Jsonp } from '@angular/http';

@Injectable()
export class ProductService {

    private urlPage = 'http://api.zanox.com/json/2011-03-01/products?q=iphone&connectid=XXXXXXXXXXXX&programs=12011&callback=JSONP_CALLBACK';

    constructor(private _jsonp: Jsonp) { }

    getPage(): Observable<Page> {
        return this._jsonp.get(this.urlPage).map(this.extractData).catch(this.handleError);
    }

    private extractData(res: Response) {
        let body = res.json();
        return body || {};
    }

    private handleError(error: any) {
        let errMsg = (error.message) ? error.message :
            error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        console.error(errMsg);
        return Observable.throw(errMsg);
    }

}

And I'm getting this error:

Uncaught ReferenceError: __ng_jsonp____req0_finished is not defined at products?q=iphone&connectid=XXXXXXXXXX&programs=12011&callback=__ng_jsonp____req0_finished:1

(anonymous) @ products?q=iphone&connectid=XXXXXXXXXXXX&programs=12011&callback=__ng_jsonp____req0_finished:1 common.js:143

Uncaught TypeError: Cannot read property 'apply' of null at globalOnerror (common.js:143)

globalOnerror @ common.js:143

productService.ts:29 200 - Ok

How to solve that?

2条回答
Lonely孤独者°
2楼-- · 2019-03-02 19:21

I solved this bug going back to the version 2.3.1 of Angular. The bug happens in the version 2.4.X.

"@angular/common": "~2.3.1",
"@angular/compiler": "~2.3.1",
"@angular/core": "~2.3.1",
"@angular/forms": "~2.3.1",
"@angular/http": "~2.3.1",
"@angular/platform-browser": "~2.3.1",
"@angular/platform-browser-dynamic": "~2.3.1",
"@angular/router": "~3.3.1",
查看更多
来,给爷笑一个
3楼-- · 2019-03-02 19:23

Here is my solution:

set a variable:times
export class WikipediaService {
  constructor(private jsonp: Jsonp) {
    this.times=0;}
  search (term: string) {
    let wikiUrl = 'http://en.wikipedia.org/w/api.php';
    let params = new URLSearchParams();
    params.set('search', term); // the user's search value
    params.set('action', 'opensearch');
    params.set('format', 'json');
    params.set('callback', `__ng_jsonp__.__req${this.times}.finished`);
    this.times=this.times+1;
    // TODO: Add error handling
    return this.jsonp
           .get(wikiUrl, { search: params })
           .map(response => <string[]> response.json()[1]);
  }
}
查看更多
登录 后发表回答