使用GET方法的Jquery中的CORS问题 [英] CORS issue in Jquery with GET method

查看:61
本文介绍了使用GET方法的Jquery中的CORS问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过具有GET方法的URL获取数据.我已经使用Jquery来获取JSON并将其解析为HTML表.

I am working on getting a data through a URL with GET method available. I have used the Jquery to get the JSON and parse it into HTML table.

但是,我无法解决CORS问题.我添加了代码:

However, I cannot fix the CORS issue. I have added the code:

res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Credentials', 'True');
res.header('Access-Control-Allow-Headers', 'accept, content-type, x-parse-application-id, x-parse-rest-api-key, x-parse-session-token');

我还安装了chrome扩展程序以启用CORS.

I have also installed the chrome extension to enable CORS.

但是,它们都不起作用.我也尝试过直接使用XMLHttpRequest,它仍然无法正常工作...

However, none of them worked. I have also tried to use XMLHttpRequest directly, it still doesn't work...

这是我使用Jquery的代码:

Here is my code using Jquery:

<!DOCTYPE html>
<html>
<head><meta charset="utf-8"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
 var url = 'https://www.probancemail.com/rest/v2/stats/RT1-PREM_DE?&token=5eX8dIljx9fWOvFu7WO22uL3EEYSN8PEciwZKdYqKxK6HOsHxjyYQpVBQiSLEGOJkId9wTNOAUpGRPfPWJgXV5u8s9EUL9hVeGSa'
function FIXCORS(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Credentials', 'True');
res.header('Access-Control-Allow-Headers', 'accept, content-type, x-parse-application-id, x-parse-rest-api-key, x-parse-session-token');
 // intercept OPTIONS method
if ('OPTIONS' == req.method) {
  res.send(200);
}
else {
  next();
}
};
$(document).ready(function () {
$.getJSON(url,
function (json) {
tr = $("<tr></tr>")
for (var i = 0; i < json.results.length; i++) {
var td = "<td>" + json.results[i].address_components[0].long_name+"</td>"
$(tr).append(td);
}
$('table').append($(tr));
});
});
</script>
</head>
<body>
<table></table>
</body>
</html>

它总是显示请求的资源上不存在"Access-Control-Allow-Origin"标头的错误消息.因此,不允许访问原始空".

It always shows the error message of No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

这是我的直接基于HTTP请求的代码,它基于本文: https://www.html5rocks.com/zh-CN/tutorials/cors/

And is my code using HTTP Request directly, which is based on this article: https://www.html5rocks.com/en/tutorials/cors/

<!DOCTYPE html>
<html><meta charset="utf-8"/>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>

function reqListener() {
console.log(this.responseText);
}
// Create the XHR object.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}

// Helper method to parse the title tag from the response.
function getTitle(text) {
return text.match('/campaign');
}

// Make the actual CORS request.
function makeCorsRequest() {
// This is a sample server that supports CORS.
var url = 'https://www.probancemail.com/rest/v2/stats/RT1-PREM_DE?&token=5eX8dIljx9fWOvFu7WO22uL3EEYSN8PEciwZKdYqKxK6HOsHxjyYQpVBQiSLEGOJkId9wTNOAUpGRPfPWJgXV5u8s9EUL9hVeGSa';

var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS not supported');
return;
 }

 // Response handlers.
xhr.onload = function() {
var text = xhr.responseText;
var title = getTitle(text);
alert('Response from CORS request to ' + url + ': ' + title);
 };

xhr.onerror = function() {
alert('Woops, there was an error making the request.');
};
 xhr.send();
}
</script>
</head>
<body>

<div id = "div"></div>
<button type = "button" onclick = "makeCorsRequest">Retrieve Report Data</button>

</body>
</html>

此人没有错误消息,但根本没有结果...有没有人有想法?欢迎任何反馈!!!提前非常感谢!

This one doesn't have error message but has no outcome at all... Is there anyone have some thoughts? Any feedback is welcomed!!! Thanks a lot in advance!

推荐答案

CORS.如果发送跨域请求,则jQuery将 Origin:http://example.com 标头添加到 request ,并期望使用 Access-Control-Allow-Origin:响应中的http://example.com Access-Control-Allow-Origin:* 标头.

CORS should be supported on the server. In case of sending cross-domain request, jQuery adds Origin: http://example.com header to request and expects Access-Control-Allow-Origin: http://example.com or Access-Control-Allow-Origin: * header in response.

您可以在MDN上了解有关CORS的更多信息: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

You can read more about CORS at MDN: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

P.S.关于脚本中的 FIXCORS 函数,我假设您已经从某些 Node.js CORS指南中复制了它,它也应该放在服务器上

P.S. Regarding FIXCORS function in your script, I assume you have copied that from some Node.js CORS guide, it should be place on the server as well

这篇关于使用GET方法的Jquery中的CORS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆