Google Cloud Functions 启用 CORS? [英] Google Cloud Functions enable CORS?
本文介绍了Google Cloud Functions 启用 CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚完成了 Hello World Google Cloud Functions 教程并收到了以下响应标头:
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
如何添加 CORS 标头以便能够从我的网站调用我的函数?
How can I add the CORS headers to be able to call my function from my website?
推荐答案
我们开始:
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!);
};
然后你就可以像往常一样使用 jquery/whatever 了:
then you can jquery/whatever it as usual:
$.get(myUrl, (r) => console.log(r))
这篇关于Google Cloud Functions 启用 CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文