简体   繁体   English

如何从AJAX格式的API中提取?

[英]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: 现在让我更详细地解释一下:

  1. The above code uses jQuery, which is just a Javascript library that you can include in your page. 上面的代码使用jQuery,这只是一个Javascript库,您可以将其包含在页面中。
  2. jQuery itself has a bunch of utility functions that make it easy(er) to make an Ajax request (which is what you need in this case). jQuery本身具有许多实用程序函数,这些函数使发出Ajax请求(在这种情况下正是您所需要的)变得更容易。 So the line that says $.ajax(... is just me calling the jQuery function to make ajax requests. 所以说$.ajax(... )的行只是我调用jQuery函数来发出ajax请求。
  3. Since this is an Ajax request to another third-party website, you need to initiate what's called a Cross Domain Resource Sharing ( CORS ) Ajax request. 由于这是对另一个第三方网站的Ajax请求,因此您需要启动所谓的跨域资源共享( CORS )Ajax请求。 In order to do this, you need to specify some special parameters in your function call. 为此,您需要在函数调用中指定一些特殊参数。 For example, you need to specify that the data type that you expect to receive back is of type JSONP (essentially, you are asking to receive data in Javascript Object Notation format - JSON) 例如,您需要指定希望接收回的数据类型为JSONP类型(实际上,您要求接收Javascript Object Notation格式-JSON的数据)
  4. Finally, the data returned by the website you linked defines a callback function called 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM