如何使用 javascript 和 jQuery 从 JSON 检索数据?
How to retrieve data from JSON with javascript and jQuery?
好的,所以我一直在尝试从该网站检索 JSON 中的单个字符串:
http://blog.teamtreehouse.com/api/get_recent_summary/?count=1
这是我的片段:
<html>
<head>
<title>Simple Page</title>
</head>
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$.getJSON('http://blog.teamtreehouse.com/api/get_recent_summary/?count=1', function(data)
{
alert(data.title)
});
});
</script>
</html>
只是想获得一个简单的标题并将其显示为提醒,或者只是将其写在网站上,但出于某种原因我做不对。我还检查了我的 jquery 是否正常工作,所以不可能。
提前致谢!
查看从提供的端点返回的数据,您的回调应该显示为 alert(data.posts[0].title)
对于此类错误,您还需要传递 header 参数以及请求检查链接...
How to allow CORS?
Getting Cross Origin Block Request [CORS] error when using .getJSON to get Play Store App Details
No 'Access-Control-Allow-Origin' header is present on the requested resource.
缺少 CORS HTTP header,因此您无法直接执行 AJAX 请求。您需要使用服务器端代理或使用 http://crossorigin.me/ 服务从远程网站获取数据。
您正在访问的服务器提供了JSONP支持,因此您可以通过将callback=?
添加到URL
将其转换为JSONP调用
$.getJSON('http://blog.teamtreehouse.com/api/get_recent_summary/?count=1&callback=?', function(data)
{
alert(data.posts[0].title)
});
如果不支持 JSONP,您可以使用 crossorigin.me 服务,但我不会依赖此服务进行生产使用。
$.getJSON('http://crossorigin.me/http://blog.teamtreehouse.com/api/get_recent_summary/?count=1', function(data)
{
alert(data.posts[0].title)
});
好的,所以我一直在尝试从该网站检索 JSON 中的单个字符串: http://blog.teamtreehouse.com/api/get_recent_summary/?count=1
这是我的片段:
<html>
<head>
<title>Simple Page</title>
</head>
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$.getJSON('http://blog.teamtreehouse.com/api/get_recent_summary/?count=1', function(data)
{
alert(data.title)
});
});
</script>
</html>
只是想获得一个简单的标题并将其显示为提醒,或者只是将其写在网站上,但出于某种原因我做不对。我还检查了我的 jquery 是否正常工作,所以不可能。
提前致谢!
查看从提供的端点返回的数据,您的回调应该显示为 alert(data.posts[0].title)
对于此类错误,您还需要传递 header 参数以及请求检查链接...
How to allow CORS? Getting Cross Origin Block Request [CORS] error when using .getJSON to get Play Store App Details
No 'Access-Control-Allow-Origin' header is present on the requested resource.
缺少 CORS HTTP header,因此您无法直接执行 AJAX 请求。您需要使用服务器端代理或使用 http://crossorigin.me/ 服务从远程网站获取数据。
您正在访问的服务器提供了JSONP支持,因此您可以通过将callback=?
添加到URL
$.getJSON('http://blog.teamtreehouse.com/api/get_recent_summary/?count=1&callback=?', function(data)
{
alert(data.posts[0].title)
});
如果不支持 JSONP,您可以使用 crossorigin.me 服务,但我不会依赖此服务进行生产使用。
$.getJSON('http://crossorigin.me/http://blog.teamtreehouse.com/api/get_recent_summary/?count=1', function(data)
{
alert(data.posts[0].title)
});