jQuery $.getJSON 请求中没有“Access-Control-Allow-Origin”标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29862490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:38:39  来源:igfitidea点击:

No 'Access-Control-Allow-Origin' header on $.getJSON request

jqueryajaxjsonhttp-headers

提问by Lindsay

I am trying to make a simple $.getJSONrequest to the following URL: http://dev.markitondemand.com/Api/v2/InteractiveChart/json?parameters=%7B%22Normalized%22%3Afalse%2C%22NumberOfDays%22%3A7%2C%22DataPeriod%22%3A%22Day%22%2C%22Elements%22%3A%5B%7B%22Symbol%22%3A%22NUS%22%2C%22Type%22%3A%22price%22%2C%22Params%22%3A%5B%22c%22%5D%7D%5D%7D

我正在尝试向$.getJSON以下 URL发出简单请求:http: //dev.markitondemand.com/Api/v2/InteractiveChart/json?parameters=%7B%22Normalized%22%3Afalse%2C%22NumberOfDays%22%3A7% 2C%22DataPeriod%22%3A%22Day%22%2C%22Elements%22%3A%5B%7B%22Symbol%22%3A%22NUS%22%2C%22Type%22%3A%22price%22%2C%22Params% 22%3A%5B%22c%22%5D%7D%5D%7D

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:1337' is therefore not allowed access.

请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:1337'。

My code is pretty straightforward:

我的代码非常简单:

 var ticker = "CRR";
 var url = "http://dev.markitondemand.com/Api/v2/InteractiveChart/json?parameters=%7B%22Normalized%22%3Afalse%2C%22NumberOfDays%22%3A7%2C%22DataPeriod%22%3A%22Day%22%2C%22Elements%22%3A%5B%7B%22Symbol%22%3A%22" + ticker + "%22%2C%22Type%22%3A%22price%22%2C%22Params%22%3A%5B%22c%22%5D%7D%5D%7D";

 $.getJSON(url, function(data) {
     console.log(data);
 }).success(function() {
     $('#show-data').html("Successfully retrieved data.");
 }).error(function() {
     $('#show-data').html("Service Unavailable.");
 });

It's not running any error or success functions, and I'm getting the same thing when I try $.ajaxinstead. As far as my setup goes, I'm experimenting with a MEN project, so Mongo, Express, and Node.js on my localhost. Any help would be appreciated.

它没有运行任何错误或成功功能,当我尝试时,我得到了同样的结果$.ajax。就我的设置而言,我正在试验一个 MEN 项目,比如我的本地主机上的 Mongo、Express 和 Node.js。任何帮助,将不胜感激。

回答by jfriend00

You are apparently attempting a cross-origin Ajax request. That means you're trying to contact a server on a different domain/port than the one that the originating webpage is on. This is called a cross origin request and is not permitted by default. You can read about a browser's same origin security policy here.

您显然是在尝试跨域 Ajax 请求。这意味着您正在尝试联系与原始网页所在域/端口不同的域/端口上的服务器。这称为跨源请求,默认情况下是不允许的。您可以在此处阅读有关浏览器同源安全策略的信息

In order to be allowed to make a cross origin request directly, the server that you are making the request to must specifically allow it.

为了被允许直接发出跨域请求,您发出请求的服务器必须明确允许它。

The Access-Control-Allow-Originheader is one way that it would allow this type of access, but it is apparently not applying that header and thus the request is being denied by the browser. You can read about how CORS (cross origin resource sharing) works here.

所述Access-Control-Allow-Origin报头是一种方式,它可以使这种类型的访问,但它显然不应用该标头,因此该请求正在被浏览器拒绝。您可以在此处阅读 CORS(跨源资源共享)的工作原理。

Other ways to make cross origin requests are using JSONP (also requires cooperation from the destination server to support a JSONP request) or via a proxy (another server that you are allowed to contact that can make the request to the desired server for you and return you the results). The proxy requires your own server code on a server you do have permission to reach, but does not require cooperation from the target server.

其他发出跨源请求的方法是使用 JSONP(也需要目标服务器的合作以支持 JSONP 请求)或通过代理(允许您联系的另一台服务器,该服务器可以为您向所需的服务器发出请求并返回)你的结果)。代理需要您有权访问的服务器上的您自己的服务器代码,但不需要目标服务器的合作。

Per the doc on this page, it appears that Markit On Demand does support JSONP so you could use that form for the cross origin request. jQuery's ajax supports that format if you set the appropriate dataType: "jsonp"option for $.ajax().

根据此页面上的文档,Markit On Demand 似乎确实支持 JSONP,因此您可以将该表单用于跨源请求。如果您dataType: "jsonp"$.ajax().

回答by ne1410s

This message is being seen because the server is not allowing Cross-Origin resources via Ajax. So either get the server administrator to enable cors for you or try using JSONP if this is supported by the server.

看到此消息是因为服务器不允许通过 Ajax 跨源资源。因此,要么让服务器管理员为您启用 cors,要么尝试使用 JSONP(如果服务器支持)。

http://enable-cors.org/server.html

http://enable-cors.org/server.html