Angular 2:JSONP请求给出异常:响应状态:200 Ok for URL

Angular 2:JSONP请求给出异常:响应状态:200 Ok for URL,angular,jsonp,Angular,Jsonp,我尝试使用Angular 2进行JSONP调用,并遵循了本文中提到的所有内容: 这是我的服务。ts: 这是一个JSONP调用,我在API的末尾添加了:&callback=JSONP\u callback 当调用该服务时,我会得到有效的JSON响应,如Chrome的开发者工具>网络>响应选项卡中所示: parseQuote({"quoteText":"A failure is a man who has blundered but is not capable of cashing in on t

我尝试使用Angular 2进行JSONP调用,并遵循了本文中提到的所有内容:

这是我的服务。ts:

这是一个JSONP调用,我在API的末尾添加了:&callback=JSONP\u callback

当调用该服务时,我会得到有效的JSON响应,如Chrome的开发者工具>网络>响应选项卡中所示:

parseQuote({"quoteText":"A failure is a man who has blundered but is not capable of cashing in on the experience. ", "quoteAuthor":"Elbert Hubbard", "senderName":"", "senderLink":"", "quoteLink":"http://forismatic.com/en/d74d7833cb/"})
但我也发现了以下错误:

未捕获引用错误:未定义parseQuote 异常:URL为的状态为200 Ok的响应:
请告知。我使用的是angular v2.2.1。

您的代码没有问题,问题在于您提供的api端点url。显然,它不是在回调参数中使用回调函数名,而是在jsonp参数中使用


用此url替换您的url,它应该可以正常工作:http://api.forismatic.com/api/1.0/?method=getQuote&format=jsonp&jsonp=JSONP_CALLBACK&lang=en

谢谢奥贝德。你的建议奏效了。我想问一下,您是如何知道回调函数名需要输入jsonp参数而不是回调参数的。是不是有什么错误告诉你了。如果你能分享这一点,它将有助于我在类似的情况下在未来。
 this.dataLoadService.getQuotes()
      .subscribe(data => {
        console.log(data);
        this.quotes.push(data);
      });
parseQuote({"quoteText":"A failure is a man who has blundered but is not capable of cashing in on the experience. ", "quoteAuthor":"Elbert Hubbard", "senderName":"", "senderLink":"", "quoteLink":"http://forismatic.com/en/d74d7833cb/"})