1

を使用しているページがありますwikipedia api。しかし、JSONクロスドメイン制限には使用できないため、使用しますJSONP。私のコードは次のようになります

<body>
<script type="text/javascript">
function res(data){
  alert(data);
};
</script>
<script type="text/javascript" src="http://en.wikipedia.org/w/api.php?action=opensearch&search=api&callback=res&limit=10&namespace=0&format=jsonfm">
</script>
</body>

resここでは、URL で指定されたコールバック関数を提供しています。私がfirebugで見たように、応答は次のようになりました

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>MediaWiki API Result</title>
</head>
<body>
<br />
<small>
You are looking at the HTML representation of the JSON format.<br />
HTML is good for debugging, but probably is not suitable for your application.<br />
See <a href='http://www.mediawiki.org/wiki/API'>complete documentation</a>, or
<a href='/w/api.php'>API help</a> for more information.
</small>
<pre>
res([
&quot;api&quot;,
[
&quot;API&quot;,
&quot;Apiales&quot;,
&quot;Apiaceae&quot;,
&quot;Apia&quot;,
&quot;Apicomplexa&quot;,
&quot;Apicomplexa lifecycle stages&quot;,
&quot;Apidae&quot;,
&quot;APIA Leichhardt Tigers&quot;,
&quot;Apical membrane&quot;,
&quot;Apical consonant&quot;
]
])
</pre>
</body>
</html> 

これは、サーバーがコールバック関数に含まれる応答を送信していることを意味しますがres、この関数はブラウザーによって実行されません (alert呼び出されていません)。Firefox 5 を使用しています。

4

1 に答える 1

2

それは応答のすぐそこにあると言います:

JSON 形式の HTML 表現を見ています。
HTML はデバッグには適していますが、おそらくアプリケーションには適していません。

試す

<script type="text/javascript" src="http://en.wikipedia.org/w/api.php?action=opensearch&search=api&callback=res&limit=10&namespace=0&format=json">
于 2011-07-27T14:27:04.037 に答える