0

I am trying to write some simple Javascript to load a JSON payload from a URL.

I know I have CORS issues, and for the moment I'm fine with that. I can disable chrome's check.

The problem I have is that I get back a 200 OK response, but also an error and I don't understand why.

var urlAddress = "https://light-ratio-149809.appspot.com/ESI-OrgUnitService?jsoncallback=?";
$.getJSON(urlAddress, {
  format: "json"
})
.always(function( jqXHR, textStatus, errorThrown ) { 
  console.log("reponse textStatus["+textStatus+"] errorThrown["+errorThrown+"] [" + jqXHR.responseText + "]"); 
})
.done(function(data) {
  console.log("success");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Can someone please tell me where I'm going wrong?

If I use this JSON payload, it works.

https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json

The error looks to be a parse error.

reponse textStatus[parsererror] errorThrown[Error: jQuery331004379421802427674_1551456238952 was not called] [undefined]

ADyson
  • 57,178
  • 14
  • 51
  • 63
jeff porter
  • 6,560
  • 13
  • 65
  • 123
  • 6
    what exactly is the error? – Isaac Vidrine Mar 01 '19 at 15:52
  • Open the console. Look at the error message reported. – Quentin Mar 01 '19 at 15:53
  • 1
    probably the return text is not json and fails to be parsed. classic jQuery trap – Zzirconium Mar 01 '19 at 15:53
  • 2
    if you are returning JSON why does it have the jsonp parameter on the URL? – epascarello Mar 01 '19 at 15:55
  • 3
    the response is valid JSON but it ain't valid JSONP. You can't get round CORS restrictions by pretending the server returns a JSONP response when it doesn't – ADyson Mar 01 '19 at 15:56
  • 1
    Only a passing familiarity with jquery ajax, but this might help https://stackoverflow.com/questions/11736431/make-cross-domain-ajax-jsonp-request-with-jquery -- It might not like the jsonp param since I assume that getJson assumes you're just getting json not a jsonp request, which expects a callback – Thomas Prince Mar 01 '19 at 15:57
  • add a .always(function( jqXHR, textStatus, errorThrown ) { console.log(jqXHR.responseText); }); to see what puzzles it – Zzirconium Mar 01 '19 at 16:00
  • 2
    the parseError is because jQuery thinks you're expecting a JSONP response (i.e. some executable Javascript), not just data. It thinks that because you added the jsonCallback querystring parameter. Once you remove that, it goes back to expecting regular JSON, but is then defeated by the server's CORS restrictions. As I said, you can't get round CORS by pretending you want to receive JSONP when that's not actually what the server is returning. I suggest you find out whether this server does in fact have a JSONP endpoint, or whether your app can be added as an allowed CORS client. – ADyson Mar 01 '19 at 16:02
  • P.S. a small side point: I don't think `format: "json"` is useful. It doesn't seem to affect what the remote server returns in this case. I would assume the server isn't expecting that parameter and just ignores it. – ADyson Mar 01 '19 at 16:09

1 Answers1

1

The parseError is because jQuery thinks you're expecting a JSONP response (i.e. some executable Javascript), not just JSON data. It thinks that because you added the jsonCallback querystring parameter to the URL.

Once you remove that, it goes back to expecting regular JSON (which is what the endpoint returns), but is then defeated by the server's CORS restrictions.

You can't get round CORS by pretending you want to receive JSONP when that's not actually what the server is returning. I suggest you find out whether this server does in fact have a JSONP endpoint, or whether your app can be added as an allowed CORS client.

ADyson
  • 57,178
  • 14
  • 51
  • 63