Reputation: 135
I'm trying to make this work but I don't understand why this always returns "Request Failed"? The json seems to be valid (I know jQuery is strict about this), maybe it's because of the httpS?
var geo_url = "https://spapi.cdnspstr.com/api/get_geo_ip";
var jqxhr = $.getJSON(geo_url, {
format: "json"
})
.done(function (json_data) {
alert("currency: " + json_data.data.currency);
})
.fail(function () {
alert("Request Failed");
});
Upvotes: 0
Views: 1930
Reputation: 3848
The request should be done using jsonp as cross-domain json-ajax requests are not allowed
$.ajax("https://spapi.cdnspstr.com/api/get_geo_ip",{
dataType: 'jsonp',
success: function(json_data) {
alert("currency: " + json_data.data.currency);
},
error: function() {
alert("Request Failed");
}
});
Upvotes: 1
Reputation: 48972
It's maybe due to JSON hijacking. If that is the case, you have to explicitly allow get JSON on server side.
I don't know which technology you use on server side. If you're using ASP.NET MVC, you have to allow get JSON using JsonRequestBehavior.AllowGet
Upvotes: 0
Reputation: 10994
var geo_url = "https://spapi.cdnspstr.com/api/get_geo_ip";
$.ajax({
url: geo_url,
data: {
format: "json"
},
crossDomain: true,
dataType: 'jsonp',
success: function (json_data) {
alert("currency: " + json_data.data.currency);
alert("city: " + json_data.data.city);
},
error: function () {
alert("Request Failed");
}
});
Upvotes: 1
Reputation: 8623
When in doubt, debug:
var geo_url = "https://spapi.cdnspstr.com/api/get_geo_ip";
var jqxhr = $.getJSON(geo_url, {
format: "json"
})
.done(function (json_data) {
alert("currency: " + json_data.data.currency);
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
alert("Request Failed");
});
If there is any error in the backend (500, 403, ...), you will get Request Failed
anyway.
Upvotes: 0