[英]Runkit - No 'Access-Control-Allow-Origin' header is present on the requested resource
var cors = require("cors");
cors({ origin: '*' });
cors({ allowHeaders: 'X-PINGOTHER'});
cors({ methods: 'GET,HEAD,PUT,PATCH,POST,DELETE'});
exports.endpoint = function(request, response) {
let text = '100,000';
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.end(text);
}
I am running this on Runkit and still get the error when checking on a website, where I want to display this return value: "No 'Access-Control-Allow-Origin' header is present on the requested resource" 我正在Runkit上运行此文件,但在要显示此返回值的网站上进行检查时仍然出现错误:“所请求的资源上没有'Access-Control-Allow-Origin'标头”
In your example you've loaded the cors
module and configured it, but not actually done anything to get it to intercept the HTTP request and send back your CORS headers. 在您的示例中,您已经加载了
cors
模块并对其进行了配置,但实际上并未做任何事情来使其拦截HTTP请求并发送回CORS标头。
If you're just using a simple Runkit endpoint, you don't need the CORS module at all – just add the headers in your endpoint, where you're already adding the Content-Type
header: 如果您仅使用一个简单的Runkit端点,则根本不需要CORS模块-只需在已添加
Content-Type
头的端点中添加头即可:
exports.endpoint = function(req, res) {
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
});
res.end('foo');
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.