0

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");
});
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
NicoF
  • 121
  • 3
  • 11
  • If you are trying to access a file from a different domain, then your subject to the [*same-origin policy*](http://www.w3.org/Security/wiki/Same_Origin_Policy). If the server does not enable CORS you cannot load the file via Ajax. – Felix Kling Jul 18 '13 at 09:46
  • 2
    I am able to make the request using jsonp, which allows cross domain JSON requests. http://jsfiddle.net/p2bra/ – Kevin Bowersox Jul 18 '13 at 09:52

4 Answers4

1
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");
    }
});

FIDDLE

Spokey
  • 10,974
  • 2
  • 28
  • 44
1

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");
    }
});
Loken Makwana
  • 3,788
  • 2
  • 21
  • 14
  • *"cross-domain json-ajax request are not allowed"*: Cross-domain Ajax requests (no matter whether JSON or not) are not allowed by *default* but they can be if the server enables CORS. – Felix Kling Jul 18 '13 at 09:58
  • I tested this code & it works...I guess the ajax call with 'jsonp' does the trick here – NicoF Jul 18 '13 at 10:05
0

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

Community
  • 1
  • 1
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
  • Uh? Why does this have anything to do with JSON hijacking? – Felix Kling Jul 18 '13 at 09:50
  • @Felix Kling: I'm working with ASP.NET MVC and I have to use `JsonRequestBehavior.AllowGet`, otherwise javascript cannot load the JSON even on the same domain. The reason for this is JSON hijacking. – Khanh TO Jul 18 '13 at 09:51
  • Ok, but what if the service uses PHP on the backend? The statement *"Due to JSON hijacking, you have to explicitly allow get JSON on server side."* is simply wrong because it is generalizing. The actual problem is probably the same-origin policy. – Felix Kling Jul 18 '13 at 09:53
0

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.

Hieu Nguyen
  • 8,563
  • 2
  • 36
  • 43