简体   繁体   中英

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"

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.

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:

exports.endpoint = function(req, res) {
    res.writeHead(200, {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': '*',
    });
    res.end('foo');
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM