本文介绍了speedtest.net api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个小部件来显示用户最近的速度测试结果。 speedtest.net是否有我可以使用的api?我尝试了一个ajax请求来但出现了跨域错误。我尝试了一个iframe,但这只是让它出现在下载中。

I want to make a widget to display the user's most recent speed test results. Does speedtest.net have an api I could use? I tried making an ajax request to http://speedtest.net/csv.php?csv=1&ria=0&s=0 but got a cross domain error. I tried an iframe, but that just made it appear in downloads.

这将在Google Chrome浏览器扩展程序中使用,因此我可以根据需要使用chrome API。

This is going to be in a Google Chrome extension so I can use the chrome api if necessary.

推荐答案

speedtest.net由。不幸的是,他们没有提供任何可用于speedtest.net的公共API。您可以使用它。

speedtest.net is run by Ookla and their Speed Test application. Unfortunately they don't provide any public APIs for speedtest.net which you could use.

虽然我怀疑这些满足您的需求,但它们确实提供和(其中包括CSV导出功能)。

Although I doubt either of these meet your needs, they do provide Speed Test Mini and a hosted reporting solution for their full Speed Test software package (which includes CSV exporting capabilities).

您无法使用AJAX的原因是因为Chrome不会允许JavaScript执行跨网站请求,除非响应头设置在speedtest.net的响应中,以允许这样的请求。

The reason you're unable to use AJAX is because Chrome will not allow JavaScript to perform cross-site requests unless the Access-Control-Allow-Origin response header is set in the response from speedtest.net to permit such a request.

然而,在Chrome扩展程序中,您可以通过将网址添加到文件。例如:

In a Chrome extension, however, you can allow cross-origin requests by adding the URL to the permissions section of your manifest.json file. For example:

"permissions": [
  "http://*/"
],

然后,您可以使用一些jQuery将CSV数据检索为字符串(请参阅):

You could then use a bit of jQuery to retrieve the CSV data as a string (see this answer):

$.get('http://speedtest.net/csv.php?csv=1&ria=0&s=0', function(data) {
    var csv = new String(data);
    // do stuff with csv
}, dataType='text');

这篇关于speedtest.net api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 14:17