I’m trying to get data from Geobytes. One of the templates returns JSON and I need to cross-domain access it.
I wrote these 2 functions
function getCountry(ip) {
var surl = "http://www.geobytes.com/IpLocator.htm?GetLocation&template=json.txt";
$.ajax({
url: surl,
data: '{"ipaddress":"' + ip + '"}',
dataType: "jsonp",
processData: false,
jsonpCallback: "jsonpcallback",
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
}
function jsonpcallback(rtndata) {
alert(rtndata.message);
}
The call is executed successfully, these are my response headers:
HTTP/1.1 200 OK
Date: Sat, 17 Nov 2012 12:43:54 GMT
Expires: 0
Content-type: text/html
Transfer-Encoding: chunked
the returned data is JSON, but I get
warning: Resource interpreted as Script but transferred with MIME type text/html: "http://www.geobytes.com/IpLocator.htm?GetLocation&template=json.txt&callback=jsonpcallback&{%22ipaddress%22:%22200.167.254.166%22}&_=1353148931121"
Error on the remote IpLocator.htm: Uncaught SyntaxError: Unexpected token :
The error is thrown on the returned data at
{"geobytes":{"countryid":117,
I think is maybe because it’s 117 and not "117" but I obviously can’t control the returned data. Tried to add a "processData=false" but that didn’t help.
I’ve added the error handling to the ajax and get "parsererror" on the status
How can I fix this?
Thank you to no.andrea for pointing me to the right direction!
As I’m using MVC3 I’ve added a new controller
and updated my JQuery function to
And that fixed all my problems 🙂