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);
}
我在 Runkit 上运行它,但在检查网站时仍然收到错误,我想在网站上显示此返回值: “请求的资源上不存在‘Access-Control-Allow-Origin’ header ”
最佳答案
在您的示例中,您已加载 cors
模块并对其进行配置,但实际上并未执行任何操作来让它拦截 HTTP 请求并发回您的 CORS header 。
如果您只是使用简单的 Runkit 端点,则根本不需要 CORS 模块 - 只需在端点中添加 header 即可,其中您已经添加了 Content-Type
header :
exports.endpoint = function(req, res) {
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
});
res.end('foo');
};
关于json - Runkit - 请求的资源上不存在 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48295475/