user969228
user969228

Reputation: 141

JSON call fails with JQuery

I've been searching arround and I can't retrive JSON information from this example. Can anybody help me please?

var jsonURL = "http://mdc2.cbuc.cat/dmwebservices/index.php?q=dmGetCollectionList/json";

var jqxhr = $.getJSON(jsonURL, function(data) {
  alert("Success!");
  alert(data[0].alias); 
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

jqxhr.complete(function(){ alert("second complete"); });

I've checked the URL and everywhere it says that is valid and well formated...

Upvotes: 0

Views: 125

Answers (2)

Kevin B
Kevin B

Reputation: 95022

Since that request is not returning proper jsonp, the browser can't interpret it.

If you have access to that server, it would need to be modified to accept a callback function such as ?callback=cbfunc which would then wrap the json response in a callback function, such as cbfunc(["foo","bar"]);

If you do not have access to that server, you can either use a 3rd party solution such as YQL, or you can build a server-side proxy that will make the request for you. For YQL, here's a page that can help:

http://developer.yahoo.com/yql/console/#h=SELECT%20*%20FROM%20json%20WHERE%20url%3D%22http%3A//mdc2.cbuc.cat/dmwebservices/index.php%3Fq%3DdmGetCollectionList/json%22

choose the json radio button, then at the bottom of the page you'll find a url. just remove the callback=cbfunc part.

This is the url that was generated:

http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20json%20WHERE%20url%3D%22http%3A%2F%2Fmdc2.cbuc.cat%2Fdmwebservices%2Findex.php%3Fq%3DdmGetCollectionList%2Fjson%22&format=json&callback=cbfunc

If the request contains any sensitive data, i would suggest against YQL and for using a server-side script to get the data.

Upvotes: 1

themel
themel

Reputation: 8895

Is the page running that code hosted on mdc2.cbuc.cat? Otherwise, you're probably running into the same origin policy.

Upvotes: 1

Related Questions