我正在尝试从 asp.net core api 获取数据到 svelte 页面。我收到自签名证书错误:
TypeError: fetch failed
at node:internal/deps/undici/undici:12500:13
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
[cause]: Error: self-signed certificate
at TLSSocket.onConnectSecure (node:_tls_wrap:1674:34)
at TLSSocket.emit (node:events:520:28)
at TLSSocket._finishInit (node:_tls_wrap:1085:8)
at ssl.onhandshakedone (node:_tls_wrap:871:12)
at TLSWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
}
}
接下来我使用 mkcert 在项目目录中创建证书和密钥,然后更新 vite.config.ts 如下所示:
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import fs from 'fs';
export default defineConfig({
plugins: [sveltekit()],
server: {
https: {
key: fs.readFileSync(`${__dirname}/cert/key.pem`),
cert: fs.readFileSync(`${__dirname}/cert/cert.pem`)
}
}
});
最后,当我启动 sveltekit 服务器时,我在浏览器中收到此错误:
TypeError: Could not convert argument of type symbol to string.
at webidl.converters.DOMString (node:internal/deps/undici/undici:1940:15)
at webidl.converters.ByteString (node:internal/deps/undici/undici:1945:35)
at Object.record<ByteString, ByteString> (node:internal/deps/undici/undici:1857:30)
at webidl.converters.HeadersInit (node:internal/deps/undici/undici:3397:67)
at Object.RequestInit (node:internal/deps/undici/undici:1914:21)
at new Request (node:internal/deps/undici/undici:4821:34)
at getRequest (file:///C:/path/to/project/Client/node_modules/@sveltejs/kit/src/exports/node/index.js:107:9)
at file:///path/to/project/Client/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:497:2
BTW mkcert -install 通知我我的平台不支持 Firefox。
为开发环境设置证书似乎是一项简单的任务,但我很沮丧,因为我已经有一段时间无法实际开发网站了。
我想从 asp.net core api 获取数据到 sveltekit 前端,但我不能,因为我收到了一些我无法理解的错误。
我自己刚刚也遇到了这个问题。我发现将
proxy: {}
添加到 server
对象可以修复它以某种方式。
export default defineConfig({
plugins: ...,
server: {
https: {
key: ...,
cert: ...
},
// this line fixes it *somehow*
proxy: {},
}
});