2

I need to make a cross-domain ajax request with ?t=bla for the server and get the following response if success:

{"success":"yeeee","blaOne":"blabla","blaTwo":"blablabla"}

Same goes for the error, just instead of yeeee, it's boooo.

Success will be if the ?t=bla and fail if the ?t=hm

I did something like this:

$.ajax({
  method: "GET",
  url: "http://...",
  crossDomain: true,
  data: {t:'bla'},
  dataType: "jsonp"
}).then(function(response){
  console.log(JSON.stringify(response));
}).catch(function(error){
  console.log(JSON.stringify(error));
})

As a result, I'm getting the Uncaught SyntaxError: Unexpected token :, but I am able to see the response on a browser's resource file with the yeeee message! And it's the one of the .catch(function(error). If I change the data: {t:'hm'} - I'm getting the appropriate(boooo) message, but again, it's the .catch(function(error).

Any ideas why is this happens and what I can do about it?

I don't have access to the server.

If there's no way to deal with this and it's a server's issue, how in this case I can access the resulted messages(can I at all?)

UPDATE:

I'm getting the No 'Access-Control-Allow-Origin' header is present on the requested resource is I use JSON instead of JSONP... The status I am getting is 200

ruhuves
  • 21
  • 2
  • You are specifying `dataType: 'jsonp'` yet the response is actually JSON, and they are not directly interchangeable. Set `dataType: 'json'` instead. If you made this change in an attempt to get around a CORS error, then that's an entirely separate issue which cannot be solved in JS alone. – Rory McCrossan Jun 05 '18 at 06:44
  • @JaromandaX nope, I don't have the `console.log` output at all... All I'm getting is the file on the server with a response (`{"ggg":"ggg"...}`) on the sources tab in chrome – ruhuves Jun 05 '18 at 06:44
  • of course, and if you do set `dataType: 'json'` the server will have to allow CORS – Jaromanda X Jun 05 '18 at 06:44
  • The status returned is not 200 or 201. Can you log "response.status" or "error.status" ? – Sampgun Jun 05 '18 at 06:45
  • Also jsonp is not the format you described above – Sampgun Jun 05 '18 at 06:45
  • @RoryMcCrossan I'm getting the the `Content-Type` error message header from the server... – ruhuves Jun 05 '18 at 06:46
  • Please edit the question to include the full error message – Rory McCrossan Jun 05 '18 at 06:46
  • @ruhuves - why are you setting `dataType: "jsonp"` if the server **doesn't send you JSONP** – Jaromanda X Jun 05 '18 at 06:46
  • I'm getting the `No 'Access-Control-Allow-Origin' header is present on the requested resource` is I use `JSON` instead of `JSONP`... The status I am getting is `200`. – ruhuves Jun 05 '18 at 06:48
  • correct, but you can't "get around CORS" ... CORS is something the server does to stop people borrowing resources – Jaromanda X Jun 05 '18 at 06:49
  • That's what I expected. You *have* to use `dataType: 'json'` here as that's what the server returns, however as you're making a cross-domain request you're bound by the SOP. Hence as the server does not return valid CORS headers for the domain you're calling from you will be blocked from dealing with the response in JS (although you will see a valid response in the console)... – Rory McCrossan Jun 05 '18 at 06:51
  • You will need to proxy the request on your local server and have the JS call that instead. Alternatively get in touch with the owners of the domain you're calling and ask them to add CORS headers to the endpoint(s) you call, although that's highly unlikely to happen. See the duplicate I marked for more information – Rory McCrossan Jun 05 '18 at 06:51
  • @JaromandaX So it's a server's "problem" and there's no workaround on this? I just want to be sure I'm getting it currently. And if indeed it is so, why am I still able to see the message? doesn't it should be blocked or something? – ruhuves Jun 05 '18 at 06:52
  • It's not a 'problem' it's a security feature to stop unauthorised stealing of content and phishing attempts. – Rory McCrossan Jun 05 '18 at 06:52
  • no, it's *your* problem, you want there resources, and they don't want you having them -as far as what's in the browser console, that's useful information for code developers ... they can see what they are doing is right, just that the server doesn't want them doing it – Jaromanda X Jun 05 '18 at 06:53
  • @RoryMcCrossan But if I use JSON, I don't get any message in the console with the response at all... If I use JSONP - I do. what am I not getting here, please? – ruhuves Jun 05 '18 at 06:54
  • As I've mentioned in several comments now, the console will always show the response. The browser will block you from accessing the data in JS due to the SOP. There is absolutely nothing you can do about this in JS as it's a security feature of the browser. – Rory McCrossan Jun 05 '18 at 06:55
  • @RoryMcCrossan Got it, thanks! – ruhuves Jun 05 '18 at 06:56
  • Thanks for the help guys! – ruhuves Jun 05 '18 at 06:56

0 Answers0