Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Functions enable CORS?

I just finished the Hello World Google Cloud Functions tutorial and received the following response headers:

Connection → keep-alive Content-Length → 14 Content-Type → text/plain; charset=utf-8 Date → Mon, 29 Feb 2016 07:02:37 GMT Execution-Id → XbT-WC9lXKL-0 Server → nginx 

How can I add the CORS headers to be able to call my function from my website?

like image 385
Federico Elles Avatar asked Feb 29 '16 07:02

Federico Elles


People also ask

How do I enable CORS in Cloud Storage?

You set a CORS configuration on a bucket by specifying information, such as HTTP methods and originating domains, that identify the types of requests the bucket can accept. You cannot manage CORS using the console. Use gsutil instead. Create a JSON file with the CORS configuration you would like to apply.

How do I enable CORS in firebase?

exampleFunction = functions. https. onRequest((request, response) => { cors(request, response, () => {}); return response. send("Hello from Firebase!"); });

Does Google Drive support CORS?

I recently found that Google Drive does accept CORS requests if a valid access token is provided.


Video Answer


1 Answers

here we go:

exports.helloWorld = function helloWorld(req, res) {     res.set('Access-Control-Allow-Origin', "*")   res.set('Access-Control-Allow-Methods', 'GET, POST');    if (req.method === "OPTIONS") {     // stop preflight requests here     res.status(204).send('');     return;   }    // handle full requests   res.status(200).send('weeee!); }; 

then you can jquery/whatever it as usual:

$.get(myUrl, (r) => console.log(r)) 
like image 167
spencercooly Avatar answered Nov 07 '22 23:11

spencercooly