I am using the following API call in jQuery to retrieve data. I’ve appended ?callback=? at the end which seems to get around the cross origin domain issue, if I don’t include that I get the cross domain error.
In chrome it says, “Resource interpreted as script but transferred with MIME type text/html.”
The response returned is Content-Type: text/html; charset=UTF-8.
I can see the response by looking at it in Chrome Console → Resources. But how can I manipulate the response in JavaScript?
$.getJSON("http://api.visistat.com/stats-api-v2.php?key=skx79q0pyu01.&qt=idd&d=json&sdate=2012-08-26&edate=2012-08-28?callback=?", function(json) {
console.log(json);
});
I do not have access to change the API.
Update:
Trying to work around using YQL e.g. http://jsfiddle.net/4VEHR/5/
Looks like this plugin may also be useful: https://github.com/padolsey/jQuery-Plugins/tree/master/cross-domain-ajax/
Unless you have control over
api.visistat.comand can modify thestats-api-v2.phpscript, there is nothing you can do besides contacting the service and asking them to fix the headers they send with the response (it needsContent-Type: text/json, while the PHP default istext/html). On the browser side, the warning you get occurs before Javascript has had any chance to work with the data — jQuery instantiates a<script>element with your API URL as its source, and the browser notices the content type mismatch immediately when the response is received, before passing it back to jQuery.This warning is harmless, though. The real problem is that the service you’re using does not support JSONP — you provide the
callbackparameter as specified in the jQuery documentation to force JSONP result expectation, but the service does not actually produce valid JSONP (it still produces plain JSON). I’ve tried changingd=jsontod=jsonpin your API request, but apparently it is not supported by the service. You will need to figure out a way to get JSONP result from the service, or implement a server-side proxy on your domain, to the avoid cross-domain issues.