4

http://portlandonline.com/shared/cfm/json.cfm?c=27321

It's returning null. I don't really have access to this. I have to have a server admin update the feed to my liking, so if you can tell me how to get this to work as is, without adding tags to my HTML please let me know. I will be using this with jQuery, and ive been trying to use getJSON which is what returns null.

$.getJSON('http://portlandonline.com/shared/cfm/json.cfm?c=27321',function(json){
    alert(json);
});

that returns null. But if i use a flickr feed for example, it works fine. it returns what it should, [onject Object]. Any ideas?

Edit: I found out it was because the HTTP response headers were set to application/json and should be set to text/html

Oscar Godson
  • 31,662
  • 41
  • 121
  • 201
  • 1
    After reading the other question, I am slightly confused: Did you mean that the Content-Type header was set to "text/html", and it should have been "application/json"? – Jonathan Jul 14 '10 at 09:52
  • Can you clarify how you determined the response headers where set to application/json and how you set those to application/xml ? Thanks – Mark Cooper May 17 '11 at 10:43
  • My backend guy told me :) however using Chrome for example, if you go to the Inspector then "type" column should say what it is. Also, if you click on one and you look under "Response Headers" you will see "Content-Type" which should help too. – Oscar Godson May 17 '11 at 20:53
  • Hmm, seems actually like the content-type for the page is (exactly) "Content-Type:application/x-javascript; charset=utf-8" – Oscar Godson May 18 '11 at 06:12

2 Answers2

4

The JSON is not valid apparently. Cut and paste it into http://www.jsonlint.com/ for more details on the syntax error. The JSON parser you are using is failing, perhaps another one will be more lenient.

Sean A.O. Harney
  • 23,901
  • 4
  • 30
  • 30
  • The parser is jQuery itself :) The problem is as you say, but the only fix is to fix the JSON itself in 1.4+, which is the better solution anyway. – Nick Craver Mar 12 '10 at 01:20
  • Yes, it looks like the server-side JSON generation is getting messed up somehow and it is not valid JSON. Perhaps you could fix it up and transform it to valid json if it is an important feed. – Sean A.O. Harney Mar 12 '10 at 01:26
  • It's because of the parenthesis at the beginning, however, most JSON APIs I see/use do this and it works. I'm not sure what I am doing differently... like look at this example: http://api.flickr.com/services/feeds/photos_public.gne?tags=programmers&tagmode=any&format=json&jsoncallback=? if you use the jQuery above, but swap out the URL with that one, it works which is also invalid. This .cfm JSON feed originally didn't have parenthesis, i added them in when I was getting NULL as i am now with them! I don't get it! – Oscar Godson Mar 12 '10 at 18:11
  • Hey thank you *so* much for that link--I'm just getting started with JSON and that solved a problem for me immediately. They should make the link for this more conspicuous on json.org. – Matt Phillips Apr 11 '12 at 03:31
1

I think the ( ) around your JSON output are what is causing the null return. I would check the json.cfm output and remove the ( )'s

Update:

The ( ) are supposed to border a remote call using jsonp.

I just ran a local test of the output in a local file and it works.

Using both:

$.getJSON("http://portlandonline.com/shared/cfm/json.cfm?c=27321", function(data){
   alert( data );
  });

and :

$.ajax({
    dataType: "jsonp", 
    url: 'http://portlandonline.com/shared/cfm/json.cfm?c=27321',
    success: function(data) {
     ...do stuff
    }   
   });

Should properly avoid any cross domain scripting issues.

i get a clean response in firebug for both these requests. the key is sending the datatype "jsonp"

More info on jsonp.

See if these help you also:

Make cross-domain Ajax requests with jQuery

Dashboard Cross-domain AJAX with jquery

Community
  • 1
  • 1
ethyreal
  • 3,659
  • 2
  • 21
  • 25
  • Thats how it originally was, and I was getting NULL then i saw: http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=? and also, saw a stackoverflow of someone suggesting () around it all, like the Flickr example (which works with the script I have above), but i'm still getting null. – Oscar Godson Mar 12 '10 at 18:13
  • seems to be a permission issue – ethyreal Mar 12 '10 at 18:53
  • Hmm, i don't get why I can't figure this out... I consider myself extremely fluent in jQuery, anyways: $.getJSON("http://portlandonline.com/shared/cfm/json.cfm?c=27321", function(data){ alert( data ); }); $.ajax({ dataType: "jsonp", url: 'http://portlandonline.com/shared/cfm/json.cfm?c=27321', success: function(data) { alert('success!'); } }); doesn't give me the success alert... could you send me all your code? my email is [FirstnameLastname(AT)gmail] Thanks, i think im just being stupid :) – Oscar Godson Mar 12 '10 at 22:24