3

Google Fonts で使用可能なすべてのフォントのリストを取得してドロップダウンに表示する必要がありますが、Google Fonts のドキュメントを理解できません。

これが私がこれまでに得たものです。

Web ブラウザーでこれにアクセスすると、予想どおり、すべてのフォントを含む json 応答が返されます。

https://www.googleapis.com/webfonts/v1/webfonts?sort=popularity&key={my-key-here}

しかし、それを ajax すると、コンソールに次のように表示されます。

XMLHttpRequest cannot load https://www.googleapis.com/webfonts/v1/webfonts?sort=popularity&key={my-key-here}. Origin {domain} is not allowed by Access-Control-Allow-Origin.

ちなみに私はjQueryを使っています。$.ajaxと の両方を試しまし$.getJSONた。

少しグーグルで調べた後、リストを取得するにはWebフォントローダーを使用する必要があるかもしれないことに気付きましたか? わかりました、十分に公平です。だから私はページにスクリプトを追加しました:

<script src="http://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js"></script>

しかし、その後は?API は特定のフォントの取得と適用のみを許可しているように見えますが、実際にはデータを返しませんよね?

誰かが私を正しい方向に向けたり、リクエストを行う方法を教えてくれますか? 返されたデータを解析して後で処理することは大したことではありませんが、実際にデータを取得する方法がわかりません。

4

2 に答える 2

3

Ok, I've had the same problem, opening the URL itself in a new window works fine, but it doesn't work as ajax request.

Your ajax/json request is correct:

https://www.googleapis.com/webfonts/v1/webfonts?key={my-key-here}

And you don't need to add this line:

<script src="http://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js"></script>

Seems like you get nothing because of google servers, not your code. If you newly created an API key or you recently activated your webfont service, give it few minutes, you will be able to use your webfont service soon

Just to confirm it for you, try the below JSON request, it works perfectly:

$(document).ready(function() {
    $.getJSON('https://www.googleapis.com/webfonts/v1/webfonts?key=MY_API_KEY', function(response) {
        //alert('json object fetched successfully!');
    });
});
于 2013-03-02T14:43:12.517 に答える
-4

Google Web Fonts サービスが提供する Web フォントの動的リストを取得するには、次のリクエストを送信します。

https://www.googleapis.com/webfonts/v1/webfonts?key=YOUR-API-KEY

出典: Google Web Fonts API - 開発者 API

于 2012-12-13T09:07:09.737 に答える