I have a website that takes a GET request and returns a JSON. I can send the website a request using a node server and receive the correct response. However, when I try to send it using client-side javascript:
$.ajax({
url: '<myurl>',
type: 'GET',
crossDomain: true,
success: function() { alert("Success"); },
error: function() { alert('Failed!'); }
});
I get the error
XMLHttpRequest cannot load <myurl>. No 'Access-Control-Allow-Origin' header
is present on the requested resource. Origin 'http://localhost:8080' is
therefore not allowed access.
When I try changing the datatype to jsonp by adding dataType: 'jsonp',
to the ajax request, I get the error
Uncaught SyntaxError: Unexpected token :
The chrome developer console tells me the error is coming from the script <myurl>&callback=jQuery21309685717469546944_1423379667604&_=1423379667605
, and when I open the script I find the JSON response I am trying to retrieve! but I guess somehow it's being interpreted as a javascript function. Any way to resolve this? I have read many SO answers and tried many approaches to no avail.