Angular 2 JSONP注入脚本未调用回调错误 [英] Angular 2 JSONP injected script did not invoke callback error
本文介绍了Angular 2 JSONP注入脚本未调用回调错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用npm服务器在localhost://3000上运行应用程序
I am running app on localhost://3000 with npm server
服务文件:
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());
}
}
组件文件:
ngOnInit(){
this._futScoreService.getCompetitions().subscribe(
(comp)=>{
console.log(comp);
},
(err)=>{
console.log(err);
}
);
}
我在控制台控制台错误中收到此错误 在网络标签上,我从API 网络标签
And I'm getting this error in console console-error and on network tab I get object from API network-tab
推荐答案
好的解决方案是通过http模块发出get请求,并为header提供get请求.标头部分是失败的主要原因.
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());
这篇关于Angular 2 JSONP注入脚本未调用回调错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文