Access to fetch at 'http://localhost:4200/posts.json' from origin 'app://obsidian.md' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
请求是从客户端发出的,但客户端是用户的设备。如果我理解正确的话,这是安全的:
import DiscoursePlugin from "./main";
import { TFile } from "obsidian";
export async function publishToDiscourse(
plugin: DiscoursePlugin,
activeFile: TFile
): Promise<{ message: string }> {
try {
const content = await plugin.app.vault.read(activeFile);
const baseUrl = plugin.settings.baseUrl;
const apiKey = plugin.settings.apiKey;
const headers = new Headers({
"Content-Type": "application/json",
"Api-Key": apiKey,
"Api-Username": "scossar", // this needs to be a setting
});
const body = JSON.stringify({
title: activeFile.name,
raw: content, // TODO: parse the content to fix internal links
category: 1, // this needs to be a setting
});
// TODO: check to see if the note has already been published
const url = `${baseUrl}/posts.json`;
const response = await fetch(url, { method: "POST", headers, body });
if (!response.ok) {
console.error("Error publishing to Discourse:", response.status);
return { message: "Error publishing to Discourse" };
}
const jsonResponse = await response.json();
// TODO: use the response to add a discoursePostId file property
console.log(`jsonResponse: ${JSON.stringify(jsonResponse, null, 2)}`);
return { message: "success" };
} catch (error) {
console.error("Error publishing to Discourse:", error);
return { message: `Error: ${error.message}` };
}
}