我正在尝试编写一个谷歌云函数来替换我的 PHP API,它当前连接到第三方应用程序以获取一些数据,然后以 JSON 格式返回数据
以下代码在本地运行该函数时有效(它返回 JSON 数据),但是,当我部署该函数时,我遇到了一个 CORBS 问题:
<块引用>跨源读取阻止 (CORB) 阻止了 MIME 类型为 text/html 的跨源响应 https://accounts.google.com/ServiceLogin?service=ah&passive=true&continue=https://uc.appengine.google.com/_ah/conflogin%3Fcontinue%3Dhttps://us-central1-phg-config-d47fb.cloudfunctions.net/favicon.ico。有关详情,请参阅 https://www.chromestatus.com/feature/5629709824032768。
const functions = require("firebase-functions");
exports.helloWorld = functions.https.onRequest((request, response) => {
response.set('Access-Control-Allow-Origin', '*');
if (request.method === "OPTIONS") {
/* handle preflight OPTIONS request */
response.set("Access-Control-Allow-Methods", "GET, POST");
response.set("Access-Control-Allow-Headers", "Content-Type");
// cache preflight response for 3600 sec
response.set("Access-Control-Max-Age", "3600");
return response.sendStatus(204);
}
// handle the main request
//response.send("main response");
var assert = require('assert')
var http = require('http')
var body = '[[BODY OF REQUEST]]';
var postRequest = {
host: "XXX",
path: "XXX",
port: 6020,
method: "POST",
headers: {
'Content-Type': 'application/moca-xml'
}
};
var req = http.request( postRequest, function( res ) {
//response.send('data returned');
var buffer = "";
res.on( "data", function( data ) { buffer = buffer + data;
} );
res.on( "end", function( data ) { console.log( buffer );
let xmlParser = require('xml2json');
response.send(xmlParser.toJson(buffer));
} );
});
req.write( body );
req.end();
});
然后我认为这可能是从 CORBS 网站返回的内容类型,所以我在输出内容之前添加了以下内容,但我仍然收到 CORBS 错误
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET, POST');
res.set('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Max-Age', '3600');
res.set('Content-Type', 'application/json');
我正在尝试做的事情是否可行,因为我不确定这是谷歌云功能应该做什么,即对谷歌平台中的事件做出反应