JSONP injected script did not invoke callback

I'm trying to get data from a Rest Api:

I tried following:

var headers: Headers= new Headers({  'dataType': 'jsonp'});
    let options = new RequestOptions({ headers: headers });

    return this.http.get('http://aaaddds.de/redmine/issues.json?limit=1200&callback=JSONP_CALLBACK', options)
      .map(this.extractData);

it doesn't work because of:

 No 'Access-Control-Allow-Origin' header

So when I try JSONP

let params = new URLSearchParams();
params.set('callback', 'JSONP_CALLBACK');
params.set('project_id', '7');
params.set('key', '42d3db30ab061a9f630df1f476c4d127f98d5ad2');
params.set('limit', '1200');


return this.jsonp.get('http://aaaddds.de/redmine/issues.json', { search: params })
  .map(this.extractData);

it says :

Uncaught ReferenceError: __ng_jsonp____req0finished is not defined JSONP injected script did not invoke callback

When I do a Jquery ajax get it works:

   $.ajax({

      url: url,
      type: 'GET',
      dataType: 'jsonp',
      success: function (datsa: any) {
        console.log(datsa);
      },
      error: function (e : any) { alert(e.toString) },
    });

I think that the parameter you provided for your callback isn't callback . In fact, this name depends on the target service and can be callback , c or something else...

To diagnose your problem, you should have a look at the Network tab of devtools in your browser to see the content of the response. It should be something like that:

__ng_jsonp__.__req0.finished({ ... })

__ng_jsonp__.__req0 is the name of the callback internally handled by Angular2.


我解决了这个问题:设置一个变量: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]);
    }
}

this._InstUrl = "your url";

let params1 = new URLSearchParams();
//params.set('search', term); // the user's search value
//params.set('action', 'opensearch');
params1.set('format', 'json');
//params1.set('callback', "ng_jsonp.__req0.finished");
params1.set('callback', "JSONP_CALLBACK");

    return this._jsonp
        .get(this._InstUrl, { search: params1 })
        .map(response => { debugger; this.Result = response.json().data })
        .subscribe(
        (data) => {
            debugger
            console.log(this.Result);
        },
        (error) => {
            debugger
            console.log(error);
        });

这一个100%工作PLZ检查

链接地址: http://www.djcxy.com/p/47560.html

上一篇: Angular 2错误

下一篇: JSONP注入的脚本没有调用回调