0

I am trying to use json data from an external provider api's. When I type their json url into the browser, it shows all the json data I need to see. However, when I try to access it using jquery, I get errors, even though google chrome shows the correct json data in the dev tools section:

enter image description here

Because of this, I assume it means that the data is actually getting retrieved? Anyway, this is the script I am using:

$.ajax({
    url: "https://some_url_here_from_different_domain.json",
    type: 'GET',
    dataType: 'jsonp',
    jsonp: 'callback',
    error: function(xhr, status, error) {
        alert(error); //<- returns Error: jQuery11020135121880332008_1387982597648 was not called
        alert(xhr.responseText); //<- returns undefined
    },
    success: function(data) { 
        alert("success");
    }
});

How do I get further error details so I can understand what's causing the error to appear?

oshirowanen
  • 15,297
  • 82
  • 198
  • 350

2 Answers2

1

Your JSONP code is fine and should work, but the endpoint you're calling in the example is returning JSON instead of JSONP regardless of your code.

Endpoint: https://localbitcoins.com/sell-bitcoins-online/national-bank-transfer/.json?callback=jQuery110202691209970507771_1387997968891&_=1387997968892

Difference: What are the differences between JSON and JSONP?

Possible solutions:

  1. Contact localbitcoins and ask them to enable JSONP for that URL
  2. Setup a proxy server to pull in the data and return JSONP yourself - good starting point might be: https://npmjs.org/package/json-proxy
Community
  • 1
  • 1
TomFuertes
  • 7,150
  • 5
  • 35
  • 49
  • Why would they provide a public api which only returns json, and not jsonp? What use is data especially when it's public if it can't be used cross domain? – oshirowanen Dec 25 '13 at 19:12
0

So far my understanding, you want to get json data using ajax call.

According to this link - Can't get json data from jQuery ajax call

Use dataType: 'json' instead of 'jsonp'

var jsonData;

$.ajax({
        url: "https://some_url_here_from_different_domain.json",
        dataType: 'json',
        success: function(response) {
            jsonData = response;
        }
});
Community
  • 1
  • 1
  • I think I need to specify jsonp because it's cross domain. Please look at the second link in my question to see what happens with I don't specify jsonp. – oshirowanen Dec 25 '13 at 16:18