I am trying to figure out how to use json url in browser and display data on my web pages using DOM. I do not get a consistent or predictable response.
I found the JSON URL in Google Calendar that shows the json response in my browser if I just type the URL in the address bar.
I found another JSON URL in business.gov that shows a different json response in my browser, if I just type the url into the bar address.,
Then I tried using jQuery to issue $ .ajax calls to consume and display both of these JSON resources.
<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/developer-calendar@google.com/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 Calendar JSON , - JSON . ( Firebug, HTTP- 200, ).
, URL JSON JSON , URL- Google Calendar jQuery.ajax, URL- business.gov jquery.ajax?
EDIT - 19 2010 ., 6:36 EST - @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');
}
}
);
dataType 'jsonp' 'script', . htmlobj2 , json. , .ajax "" , "data" nil. , , .
JSON -?
EDIT - 22 2010 ., 11:17
Ruby script , 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 script, URL- Google Calendar.
? JSON (api.business.gov Google Calendar), Ruby, Google, Javascript/jQuery .
, . API , Google Calendar , , api.business.gov , JSON JSONP.