2016-10-05 63 views
0

以下使用我的jsonp调用,出现错误Response with status: 200 Ok for URL。如果我理解正确,这意味着我的获取请求确实有效。但是,我调用数据的方法没有。您可以在下面找到我的数据链接,或者通过clicking this linkAngular 2 Jsonp Err 200

constructor(private jsonp: Jsonp){} 
stockrun(){ 

this.jsonp.get('http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=AAPL&callback=myRun') 
    .map(res => res.json()) 
    .subscribe((information) =>{ 
    console.log(information); 
    }); 

} 

我进入myRun到URL的原因是因为数据的整理方式不同,当没有回调,像http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=AAPL

我还想补充说,没有myRun我只得到错误200代码。不过,我也得到cannot find name myRun

回答

0

您的回复不是JSON。它的封装内myRun(...),所以要反序列化,你应该叫像

this.jsonp.get('http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=AAPL&callback=myRun') 
    .map(res => { 
     // Get raw text instead of parsed object. 
     let text = res.text(); 

     // Removing myRun(). 
     text = text.substring(6, text.length - 1); 

     // Return manually parsed response. 
     return JSON.parse(text); 
    }) 
    .subscribe((information) =>{ 
     console.log(information); 
    }); 

删除myRun(最后)

你将不得不调用res.text(),而不是res.json(),然后substring结果,然后调用JSON.parse()

+0

我已经试过您的解决方案,这是行不通的 – fon

0

得到了answer from this question。基本上,

var url = 'http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=AAPL&callback=JSONP_CALLBACK'; 
this.jsonp.get(url) 
.map(res => res.json()) 
.subscribe(data => console.log(data));