我正在尝试制作一个scriptr.io脚本,以便从Google+ API获取数据并将其返回到RSS Feed格式化字符串。
当我尝试运行它时,我遇到了与CORS相关的问题。
发生了错误。这很可能是跨域问题。在 如果您修改了脚本中的响应对象,请尝试添加 response.addHeaders(configuration.crossDomainHeaders)到你的代码。 您可以参考文档了解更多详细信息。
我确实在我的回复中添加了标题,但是我不知道应该如何在设置>中进行设置。配置> CORS
到目前为止,这是我的代码。
var http = require("http");
try {
// we retrieve the received json from request.parameters and we parse it since we know
// it is a stringified json
//var parameters = JSON.parse(request.parameters.params);
var parameters = {
googleApiKey: request.parameters.googleApiKey,
googlePlusFeedURL: request.parameters.googlePlusFeedURL
};
var googleApiKey = parameters.googleApiKey;
var googlePlusFeedURL = parameters.googlePlusFeedURL;
// Prepare the request to send to Google Places API
var requestParameters = {
url: googlePlusFeedURL,
params: {
//fields: ["url","object(content,attachments/url)"],
key: googleApiKey
}
};
var rep = http.request(requestParameters);
var body = JSON.parse(rep.body);
var result = jsonToXml(body);
response.write(result);
// whenever you manipulate the response object make sure to add your CORS settings to the header
console.log(configuration.crossDomainHeaders);
response.addHeaders(configuration.crossDomainHeaders);
response.close();
}
catch (e) {
return e;
}
答案 0 :(得分:0)
好的,我正在关注“响应”部分中的文档。 https://www.scriptr.io/documentation
我不知道为什么,但是移动这一行:
response.addHeaders(configuration.crossDomainHeaders);
在此之前:
response.write(result);
让我的代码正常工作。
工作代码:
var http = require("http");
try {
// we retrieve the received json from request.parameters and we parse it since we know
// it is a stringified json
//var parameters = JSON.parse(request.parameters.params);
var parameters = {
googleApiKey: request.parameters.googleApiKey,
googlePlusFeedURL: request.parameters.googlePlusFeedURL
};
var googleApiKey = parameters.googleApiKey;
var googlePlusFeedURL = parameters.googlePlusFeedURL;
// Prepare the request to send to Google Places API
var requestParameters = {
url: googlePlusFeedURL,
params: {
//fields: ["url","object(content,attachments/url)"],
key: googleApiKey
}
};
var rep = http.request(requestParameters);
var body = JSON.parse(rep.body);
var result = jsonToXml(body);
response.addHeaders(configuration.crossDomainHeaders);
response.write(result);
// whenever you manipulate the response object make sure to add your CORS settings to the header
console.log(configuration.crossDomainHeaders);
response.close();
}
catch (e) {
return e;
}