modify getJSON to work with CORS
Asked Answered
C

2

1

I am looking for ways to allow cross-domain access using $.getJSON. I came across solutions which suggest that using CORS is the solution to this problem. But most of the solutions have a general ajax format.

I cannot use JSONP since I get data from a server which I do not have access. Is there a way to modify this code using $.getJSON to get the data?

$.getJSON(jsonURL, function(res){
    console.log(JSON.stringify(res));
});

Or do I have to use ajax format for CORS?

Caesaria answered 1/4, 2014 at 19:50 Comment(0)
C
1

Server which I do not have access

I think, this will break your neck. You need some kind of access to the server or contact someone who has. At least you have to adjust the HTTP-Header to enter your domain Access-Control-Allow-Origin is the keyword.

Have a look at MDN

Casmey answered 1/4, 2014 at 20:18 Comment(5)
By Server which I do not have access I mean that I have a URL which returns data in json format. I cannot modify the content returned to a jsoncallback format.Caesaria
As I wrote: The source has to allow requests with the Access-Control-Allow-Origin. If you are allowed to access, everything should work as usual. $.getJSON(URL, f).Casmey
"At least you have to adjust the HTTP-Header to enter your domain Access-Control-Allow-Origin os the keyword." The server doesn't have to allow the origin explicitly, it can echo back whatever it receives (this is how open CORS servers do it). But yes, fundamentally you're correct: If the OP's origin is already allowed, then unless the OP Is using IE9 or earlier, $.getJSON should be working. If the OP's origin isn't allowed, then either he/she must get the server's operators to allow it, or cannot use that URL directly from a browser.Gossoon
@T.J.Crowder I am thinking of other alternatives too. Can we have a python script which can make the HTTP Post/Get request instead of using getJSON from javascript?Caesaria
@Dinesh: Yes, that's a fairly standard workaround, doing the request from your own server.Gossoon
K
0

If you have access to set the HTTP Response Headers for the page that loads your JS scripts, then YES you can use CORS to send cross-domain requests. However, this is not supported in older browsers.

You need to set the Access-Control-Allow-Origin header, e.g.

Access-Control-Allow-Origin: *

Or

Access-Control-Allow-Origin: http://host-of-other-site.com 

https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS

Kayceekaye answered 1/4, 2014 at 21:24 Comment(6)
Could you please clarify what you exactly mean by - the HTTP Response Headers for the page that loads your JS scripts. I am currently writing javascript in a HTML file to get the data.Caesaria
If you are loading www.my-server.com/your_page (where your_page contains your JS code, etc), you'd need to change your web server (apache, nginx, WEBrick, etc) to set the HTTP headers in the HTTP response for your_page, or for all pages. It's a matter of web server config and security concerns.Kayceekaye
My HTML file is on my local system - MAC desktop. So I am not clear as to what to do.Caesaria
Alternatively, can I write python script etc to get the data? The current issue is with respect to the browser - correct?Caesaria
You need to run a web server locally. If you are just opening html files in your browser you won't be able to set HTTP headers. Look at Apache, Nginx, ExpressJS, and other simple web servers.Kayceekaye
Of course, you could circumvent the problem, if the backend retrieves the data. But you need some kind of backend.Casmey

© 2022 - 2024 — McMap. All rights reserved.