JQuery - consuming JSON resources - some returned data, others not. What for?

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.

+3
3

, , . Fiddler, , .

google:

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Access-Control-Allow-Origin: *
Rest Omitted...

business.gov:

HTTP/1.1 200 OK
Date: Fri, 18 Jun 2010 21:52:10 GMT
Server: Mongrel 1.1.4
Status: 200 OK
X-Runtime: 0.36775
ETag: "172ec84fa79f748265e96d467af3d3dd"
Cache-Control: private, max-age=0, must-revalidate
Content-Type: application/json; charset=utf-8
Via: 1.1 api.business.gov
Content-Length: 229427
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Set-Cookie: .....7c5; path=/
Age: 0

[
  {"name": "Adelanto" ,
   "fips_county_cd": "71" ,
   "feat_class": "Populated Place" ,
   "county_name": "San Bernardino" ,
   "primary_latitude": "34.58" ,
   "state_name": "California" ,
..... (rest omited)

, business.gov , .

: - Google JSONP jQuery. - business.gov JSONP. Ruby ( ), - business.gov, .

+4

JSONP?
(firebug, ), , - , .

+2

: Content-Type :

Fiddler , Google Content-Type: text/plain; charset=UTF-8, business.gov << β†’ Content-Type. application/json; charset=utf-8 (, Cached).

Content-Type , - Windows. MIME-Type (, DTD HTML/XHTML, , XHTML /html Content-Type, HTML - , , XHTML, HTML.


TheJuice, , Fiddler, , :

Google Calendar

HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Expires: Tue, 22 Jun 2010 15:25:41 GMT
Date: Tue, 22 Jun 2010 15:25:41 GMT
Cache-Control: private, max-age=0, must-revalidate, no-transform
Vary: Accept, X-GData-Authorization, GData-Version
GData-Version: 1.0
Last-Modified: Tue, 22 Jun 2010 12:19:15 GMT
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Content-Length: 49803

Business.gov:

HTTP/1.1 200 OK
Date: Tue, 22 Jun 2010 15:34:33 GMT
Server: Mongrel 1.1.4
Status: 200 OK
X-Runtime: 0.37833
ETag: "172ec84fa79f748265e96d467af3d3dd"
Cache-Control: private, max-age=0, must-revalidate
Content-Type: application/json; charset=utf-8
Content-Length: 229427
Via: 1.1 api.business.gov
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
+1
source

Source: https://habr.com/ru/post/1750730/


All Articles