2017-02-09 107 views
4

我觉得这里是一个新手,但我试图运行在浏览器中一个简单的AJAX请求访问GCF和Chrome的报告:访问控制允许来源不灵谷歌云功能GCF

XMLHttpRequest无法加载 https://us-central1-bustling-opus-830.cloudfunctions.net/Authenticate。 请求的 资源上没有“Access-Control-Allow-Origin”标题。原因'https://beast.reachboarding.com.au'因此不允许 允许访问。

我有一个名为身份验证功能(如上图所示),这是使用一种称为斗:

bustling-opus-830.appspot.com 

我使用gsutil会使用以下JSON文件来设置CORS:

[{ 
    "origin": ["https://beast.localdev.com.au","*"], 
    "responseHeader": ["Content-Type"], 
    "method": ["GET", "HEAD", "DELETE", "OPTIONS"], 
    "maxAgeSeconds": 3600 
}] 

用下面的命令:

gsutil cors set cors.json gs://bustling-opus-830.appspot.com/ 

然后得到相应的GET命令的输出如下:

gsutil cors get gs://bustling-opus-830.appspot.com/ 

[{"maxAgeSeconds": 3600, "method": ["GET", "HEAD", "DELETE", "OPTIONS"], "origin": ["https://beast.localdev.com.au", "*"], "responseHeader": ["Content-Type"]}] 

我使用的是当你创建一个新的函数商提供的默认实例代码说明如下:

/** 
* Responds to any HTTP request that can provide a "message" field in the body. 
* 
* @param {!Object} req Cloud Function request context. 
* @param {!Object} res Cloud Function response context. 
*/ 
exports.helloWorld = function helloWorld(req, res) { 
    // Example input: {"message": "Hello!"} 
    if (req.body.message === undefined) { 
     // This is an error case, as "message" is required. 
     res.status(200).send('No message defined!'); 
    } else { 
     // Everything is okay. 
     console.log(req.body.message); 
     res.status(200).send(req.body.message); 
    } 
}; 

和简单使用以下Javascript的HTML:

$.ajax({ 
    url: "https://us-central1-bustling-opus-830.cloudfunctions.net/Authenticate", 
    type: "POST", 
    data: { 
     message: 'Testing' 
    }, 
    dataType: 'json', 
    success: function (response) { 
     console.log(response); 
    }, 
    error: function (xhr, status) { 
     console.log(xhr); 
    } 
}); 

这是导致错误。

在我的DEV控制台中,我可以看到网络请求通过。下面是我得到的HTTP响应头:

cache-control:private 
content-encoding:gzip 
content-length:27 
content-type:text/html; charset=utf-8 
date:Wed, 08 Feb 2017 03:45:50 GMT 
etag:W/"7-274e639a" 
function-execution-id:azc1zqfb1tq8 
server:Google Frontend 
status:200 
vary:Accept-Encoding 
x-cloud-trace-context:70e08d634a9644c32530326de0471a64;o=1 
x-cloud-trace-context:70e08d634a9644c32530326de0471a64 
x-powered-by:Express 

我本来期望看到的响应头内的访问控制允许来源标题以表示它是使*,但我肯定没有看到它。

疯狂的事情是,虽然当我在看网络项目,并点击响应我得到:

Testing 

这表明,所有的事情都是平等的,但实际上跑了!

我很抱歉,如果这已被回答,但我已经搜索了许多不同的关键字,似乎没有什么能解决我的问题。我认为在这个问题上(和一些体面的专业知识)有一双新的眼睛会有所帮助。

提前致谢!

+0

正确的gsutil命令'gsutil cors set cors.json gs:// bustling-opus-830',或者你已经确定'gsutil cors设置了cors.json gs://bustling-opus-830.appspot。 com /'应该达到同样的目的?我问,因为在https://cloud.google.com/storage/docs/cross-origin#Configuring-CORS-on-a-Bucket的文档只是使用非限定格式'gsutil cors set cors-json-file.json gs :// example' – sideshowbarker

+0

我相信这两个命令在语法上是相同的。我看到的唯一区别是JSON文件的名称。我用cors.json作为例子,它有cors-json-file.json。你是这个意思吗? – Encoder

+0

不,我的意思是'gs:// bustling-opus-830.appspot.com /'vs'gs:// bustling-opus-830' – sideshowbarker

回答

9

您的前景(HTTP)Google Cloud Function需要在对AJAX客户端请求的响应中设置适当的CORS标头。 HTTP Functions的请求和响应参数与相应的ExpressJS对象具有相同的属性,可用于设置CORS头并根据需要响应预检请求。

例如:

exports.Authenticate = function Authenticate (req, res) { 
    //set JSON content type and CORS headers for the response 
    res.header('Content-Type','application/json'); 
    res.header('Access-Control-Allow-Origin', '*'); 
    res.header('Access-Control-Allow-Headers', 'Content-Type'); 

    //respond to CORS preflight requests 
    if (req.method == 'OPTIONS') { 
     res.status(204).send(''); 
    } 

    //continue function ... 

}; 

页眉和上述逻辑应该进行修改,以反映您服务的特殊需求,但应该有希望帮助您开始。

+1

瑞恩你死定的传说!我不敢相信这是多么简单的事情。我添加了标题并点击刷新,并立即按预期执行。非常感谢! – Encoder

+0

你是我的救世主。我花了整整一天的时间来解决这个问题,这是唯一解决问题的方法。 –

相关问题