我想弄清楚如何在浏览器中使用json URL并使用DOM在我的网页中呈现数据。我没有得到一致或可预测的答复。jQuery - 使用JSON资源 - 一些返回数据,其他则不。为什么?
我发现了一个JSON URL at Google Calendar,它显示我的浏览器中的json响应,如果我只是在地址栏中输入网址。
我发现另一个JSON URL at business.gov,如果我只是在地址栏中输入URL,它会在浏览器中显示不同的json响应。 。
然后我尝试使用jQuery发出$ .ajax调用来消费和显示这两个JSON资源。
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
htmlobj=$.ajax(
{url:"http://www.google.com/calendar/feeds/[email protected]/public/full?alt=json",
async:false}
);
if (jQuery.isEmptyObject(htmlobj.responseText)===true) {
alert("htmlobj.responseText is empty");
} else {
alert("htmlobj.responseText has stuff in it");
}
$("#myDiv").html(htmlobj.responseText).fadeIn();
htmlobj1=$.ajax(
{url:"http://api.business.gov/geodata/city_county_links_for_state_of/CA.json",
async:false,
dataType:'text',
});
if (jQuery.isEmptyObject(htmlobj1.responseText)===true) {
alert("htmlobj1.responseText is empty");
} else {
alert("htmlobj1.responseText has stuff in it");
}
$("#myGovDiv").html(htmlobj1.responseText).fadeIn();
});
</script>
</head>
<body>
<h3>Google Calendar - json only</h3>
<div id="myDiv" style="display:none"></div>
<h3>Business.Gov</h3>
<div id="myGovDiv" style="display:none"></div>
</body>
Google日历JSON资源消耗良好,但是business.gov JSON资源甚至没有返回响应。 (我使用Firebug进行了检查,并在响应文本中返回了HTTP代码200)。
这两个JSON URL如何在浏览器中返回良好的JSON数据,但只有Google Calendar URL可以被jQuery.ajax使用,而business.gov URL不能被jQUery.ajax使用?
编辑 - 2010年6月19日,美国东部时间6:36 - 谢谢@Juan Manuel和@TheJuice。我试过jsonp ...这是我得到的。
如果我改变调用下面,我能够让浏览器停止阻止来自api.business.gov的反应,但我不能在数据获取(如htmlobj2是零)
htmlobj2=$.ajax(
{url:"http://api.business.gov/geodata/city_county_links_for_state_of/CA.json",
async: false,
dataType: 'jsonp',
success: function(data, textStatus) {
alert("Success");
$('#myDiv').html("Your data: ");
},
error: function (XMLHttpRequest, textStatus, errorThrown){
alert('error');
}
}
);
无论我使用'jsonp'还是'script'的dataType,我都会得到相同的结果。 htmlobj2是零,但响应标题具有整个json数据字符串。此外,如果我尝试用“data”作为参数将回调函数绑定到.ajax调用,那么“data”参数也是一个零对象。此外,成功或失败处理程序都不会被调用。
如何从响应字符串中提取此JSON数据并将其呈现在我的网页上?
编辑 - 2010年6月22日,上午11点17分
我发现了一个Ruby脚本和调整它来尝试和消费的URL。我在交互式Ruby(irb)中运行它。
require 'rubygems'
require 'json'
require 'net/http'
url = "http://api.business.gov/geodata/city_county_links_for_state_of/CA.json"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
result = JSON.parse(data)
result.each{|entry| p entry["name"] + "," + entry["full_county_name"] }
我能够使用类似的Ruby脚本来使用Google Calendar URL。
底线?我能够使用Ruby来使用JSON资源(api.business.gov和Google日历),但只能使用浏览器中使用Javascript/jQuery的Google日历资源。
我会很感激我能得到的任何见解。从网络上的任何文档或API描述看,似乎并不清楚Google Calendar日历提要为何在浏览器中运行,但api.business.gov提要在使用JSON或JSONP的浏览器中无效。
是否business.gov饲料需要在查询字符串中的特定项目(如钥匙),你不提供?那将是我的第一站。 – 2010-06-18 20:02:05
@Rob艾伦 - business.gov不要求API密钥或任何东西。事实上,如果您在浏览器地址栏中放置http://api.business.gov/geodata/city_county_links_for_state_of/CA.json,则会以JSON形式收到回复。 – 2010-06-19 05:14:30
Ruby正在运行服务器端,不会有跨域限制客户端脚本。您在这里正确的道路上...使用服务器代码来调用business.gov Web服务。 – offner 2010-06-22 17:28:40