坑了半天终于搞定了,看代码。前端代码1.app.module.tsimport{JsonpModule}来自“@angular/jsonp”;2.需要从“@angular/core”获取数据的组件{selector:"my-jsonp",templateUrl:"app/templates/tpl1.html"})exportclassAppComponent{constructor(publicjsonp:Jsonp){//重点来了,在url后面加上?callback=JSONP_CALLBACK地址letwikiUrl='http://localhost:3000/users?callback=JSONP_CALLBACK';//也可以使用URLSearchParams()来设置参数,这里只有一个参数,写在url里面。//使用map().substribe()获取数据this.jsonp.get(wikiUrl).map(res=>res.json()).subscribe((response)=>{console.log(response);},(error)=>{console.error(error);});}}后台代码(express)可以返回jsonp数据router.get('/',function(req,res,next){//使用jsonpres.jsonp({"name":"heping"});});
