Angular 2 JSONP injected script did not invoke callback error

邮差的信 提交于 2019-12-11 01:22:58

问题


I am running app on localhost://3000 with npm server

Services file:

import {Injectable} from "@angular/core";
import {Jsonp} from "@angular/http";
import 'rxjs/add/operator/map';

@Injectable()
export class futScoreService{
    constructor(private _jsonp:Jsonp){}
    getCompetitions(){

    let queryString ='?callback=JSONP_CALLBACK';
    return this._jsonp.get('http://api.football-data.org/v1/competitions/' + queryString,{method: 'Get'})
        .map((res) => res.json());
   }
}

Component file:

 ngOnInit(){
      this._futScoreService.getCompetitions().subscribe(
        (comp)=>{
          console.log(comp);
        },
        (err)=>{
          console.log(err);
        }
      );
  }

And I'm getting this error in console console-error and on network tab I get object from API network-tab


回答1:


Ok solution was making get request with http module and providing header with get request. Header part was main reason why it was failing.

let headers = new Headers({'X-Mashape-Key':'Ns0SkjyRRomshq3PgEnGoz2Zkc71p1CYnWajsnphGctvrGt46W'});
    headers.append( 'Accept', 'application/json');
        return this._http.get("http://api.football-data.org/v1/competitions/",{
        headers: headers
      })
       .map((res) => res.json());



回答2:


Angular is replacing JSONP_CALLBACK with __ng_jsonp____req0_finished but it should be __ng_jsonp__.__req0.finished

Inspect your Network response. If you see __ng_jsonp____req0_finished({...json object...}) this is the problem.

Also, some services have different requirements for the callback query string parameter, which proves to be nasty because the error is exactly the same. I was using &callback=__ng_jsonp__.__req0.finished with MailChimp which produced the same error but the response had only a json object and no callback function. This is because MailChimp's spec is to use &c= instead of &callback=

When hardcoding the Jsonp callback (re: JSONP_CALLBACK issue) you need to account for the number of calls made, as Angular persists the state of each call. An example of what I'm doing for Mailchimp:

addEmailToList(email: string, listId: string, jsonpCalls: number, callback: any) {   

const cbJsonp = '__ng_jsonp__.__req' + jsonpCalls + '.finished';

let url = [
      'http://',
      host,
      '/subscribe',
      '/post-json',
    ].join('');

let queryParams: URLSearchParams = new URLSearchParams();
queryParams.set('u', Config.MAILCHIMP_API_KEY);
queryParams.set('id', listId);
queryParams.set('EMAIL', email);
queryParams.set('c', cbJsonp);  // non-standard; varies by service; usually 'callback'
...
}



回答3:


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


来源:https://stackoverflow.com/questions/40006735/angular-2-jsonp-injected-script-did-not-invoke-callback-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!