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 中命名的回调函数。正如我在萤火虫中看到的那样,响应来了

<!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 回答