[英]How do I pull from an API with AJAX format?
How do I pull from API's in AJAX format such as these: 如何从API提取AJAX格式,例如:
http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp
I am an amateur programmer so anything in Javascript would be helpful 我是一名业余程序员,因此使用Javascript会有所帮助
The easiest way to extract this data is to use jQuery to make a cross domain ajax request. 提取此数据的最简单方法是使用jQuery进行跨域Ajax请求。 Basically, the idea is this: 基本上,这个想法是这样的:
$.ajax({
dataType: 'jsonp', //data in jsonp
contentType: "application/json; charset=utf-8",
url: 'http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp',
jsonpCallback: 'loadScoreboard',
success: function (data) {
alert(data.games.length + ' games loaded');
}
});
You'll notice that the jsonpCallback is set to loadScoreboard because that's what the jsonp data looks like. 您会注意到jsonpCallback设置为loadScoreboard,因为这就是jsonp数据的样子。
Here's a jsfiddle with a full working sample. 这是一个带有完整工作示例的jsfiddle 。
Fire up Developer tools in Google Chrome (hit F12 in your keyboard) and click on the console
tab to see the full response. 启动Google Chrome浏览器中的开发人员工具(在键盘上按F12键),然后点击console
标签以查看完整的响应。
Update: Here's another fiddle with better output results: http://jsfiddle.net/3mQh7/6/ 更新:这是另一个具有更好输出结果的小提琴: http : //jsfiddle.net/3mQh7/6/
Now let me explain in more detail: 现在让我更详细地解释一下:
$.ajax(...
is just me calling the jQuery function to make ajax requests. 所以说$.ajax(...
)的行只是我调用jQuery函数来发出ajax请求。 loadScoreboard
- you can see this by simply going to the link and analyzing the output. 最后,由您链接的网站返回的数据定义了一个名为loadScoreboard
的回调函数-您可以通过简单地转到链接并分析输出来查看此信息。 As a result, when you make the ajax request, you must name the callback function the same as the one presented in the output from the site. 结果,在发出ajax请求时,必须将回调函数命名为与站点输出中显示的回调函数相同的名称。 If you don't, jQuery automatically creates one for you with a unique identifier, but since it doesn't match the name that's imposed by the website (namely loadScoreboard
), you would get errors. 如果不这样做,jQuery会自动为您创建一个具有唯一标识符的标识符,但是由于它与网站所强加的名称(即loadScoreboard
)不匹配,因此会出现错误。 That's why you need to define the parameter jsonCallback='loadScoreboard'. 这就是为什么您需要定义参数jsonCallback ='loadScoreboard'的原因。 So the whole thing is simply making an XHTML HTTP GET Request to get a resource on a third party website that returns data in JSON format. 因此,整个过程只是发出一个XHTML HTTP GET请求,以获取返回JSON格式数据的第三方网站上的资源。 When the data is returned, a function called loadScoreboard
is expected to be called immediately with the data itself passed in as parameter to this function. 返回数据后,将立即调用名为loadScoreboard
的函数,并将数据本身作为参数传递给该函数。
If you look at my second fiddle you'll see a more elaborate example, which iterates through the results and creates a table. 如果您看我的第二个小提琴,您将看到一个更详细的示例,该示例遍历结果并创建一个表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.