modify getJSON to work with CORS

拈花ヽ惹草 提交于 2019-12-24 12:12:35

问题


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?


回答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




回答2:


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



来源:https://stackoverflow.com/questions/22795561/modify-getjson-to-work-with-cors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!