未捕获的ReferenceError:未定义__ng_jsonp____req0_finished

时间:2017-02-05 14:23:59

标签: angular typescript callback jsonp commonjs

我有一个Angular应用程序,我也在使用JSONP。

这是我的服务:

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);
    }

}

我收到了这个错误:

  

未捕获的ReferenceError:未定义__ng_jsonp____req0_finished       在产品?q = iphone&amp; connectid = XXXXXXXXXX&amp; programs = 12011&amp; callback = __ ng_jsonp ____ req0_finished:1

     

(匿名)@   产品Q = iPhone和放大器; connectid = XXXXXXXXXXXX和放大器;项目= 12011&放大器;回调= __ ng_jsonp ____ req0_finished:1   common.js:143

     

未捕获的TypeError:无法读取属性'apply'的null       at globalOnerror(common.js:143)

     

globalOnerror @ common.js:143

     

productService.ts:29 200 - 确定

如何解决?

2 个答案:

答案 0 :(得分:0)

我解决了这个错误,回到了Angular的2.3.1版本。该错误发生在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",

答案 1 :(得分:0)

这是我的解决方案:

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]);
  }
}