JSON Feed Returning null while using jQuery getJSON

独自空忆成欢 提交于 2019-12-10 09:27:44

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/2429834/json-feed-returning-null-while-using-jquery-getjson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!