openai-translator / openai-translator

基于 ChatGPT API 的划词翻译浏览器插件和跨平台桌面端应用 - Browser extension and cross-platform desktop application for translation based on ChatGPT API.
GNU Affero General Public License v3.0
23.77k stars 1.73k forks source link

[Bug] TypeError: Load Failed 在用了自己的API URL和API KEY后有这个报错,只存在于macos app端,浏览器插件正常 #669

Open exgbit opened 1 year ago

exgbit commented 1 year ago

Please search before asking

Please read README

Please check your network and OpenAI API quota

OpenAI Translator version

0.0.41

系统/浏览器版本 System/Browser version

macOS 13.2.1 (22D68)

复现步骤 Reproduce step

设置API URL和API KEY后有这个报错 这个错误只存在于macOS APP端,浏览器插件没有错误

你看到了什么错误?What errors do you see?

TypeError: Load Failed

你期望看到什么?What did you expect to see?

正常使用

还有其他的内容吗?Anything else?

没有

你是否愿意提交一份 PR 来修改这个错误?Are you willing to submit a PR?

wych42 commented 1 year ago

Same issue happens to me.

API URL and API TOKEN works on web browser extension but not on MacOS App

maltoze commented 1 year ago

尝试在你的自定义服务上添加允许跨域的 response header Access-Control-Allow-Origin: *

wych42 commented 1 year ago
image

This Option request with no auth header cause the problem.

maltoze commented 1 year ago

@wych42 https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#functional_overview

the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with the HTTP OPTIONS request method, and then, upon "approval" from the server, sending the actual request.

wych42 commented 1 year ago

@wych42 https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#functional_overview

the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with the HTTP OPTIONS request method, and then, upon "approval" from the server, sending the actual request.

Handle "options" request separately in proxy code solves this

code sample for reference(using cloudflare):

if (request.method === "OPTIONS") {
        // Handle CORS preflight requests
        return handleOptions(request);
}

const corsHeaders = {
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS",
  "Access-Control-Max-Age": "86400",
};

async function handleOptions(request) {
    // Handle CORS preflight requests.
    return new Response(null, {
      headers: {
        ...corsHeaders,
        "Access-Control-Allow-Headers": request.headers.get(
          "Access-Control-Request-Headers"
        ),
      },
    });
}