gpt4 book ai didi

visual-studio-code - WebView 中的 VSCode 跨源请求

转载 作者:行者123 更新时间:2023-12-05 04:00:54 37 4
gpt4 key购买 nike

我正在开发基于 VSCode 的扩展 WebView .扩展通过 HTTP API 与问题跟踪系统集成,例如 Jira。我想在 WebView 中呈现有关问题的信息,并创建一些表单来发表评论和更改问题状态。我不想在扩展和 WebView 之间使用消息传递。当我尝试在 WebView 中创建对 API 的 HTTP 请求时出现错误:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

服务器不支持“null”或“localhost”来源的 Access-Control-Allow-Origin。

创建从 WebView 到服务器的 HTTP 请求的方式是否忽略了缺少的 Access-Control-Allow-Origin header ?也许我可以为 WebView 面板设置一些策略?或者在扩展中创建本地主机代理并通过代理发出请求?

代码示例

import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('catCoding.start', () => {
// Create and show panel
const panel = vscode.window.createWebviewPanel(
'catCoding',
'Cat Coding',
vscode.ViewColumn.One,
{enableScripts: true}
);

// And set its HTML content
panel.webview.html = getWebviewContent();
})
}

function getWebviewContent() {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cat Coding</title>
</head>
<body>
<script>
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://google.com', true);
xhr.send();
</script>
</body>
</html>`;
}

最佳答案

来自 Github Issue 的回答

There is no vscode support for changing this. You should think of the webview more as an html view (one that does not have any server or origin) rather than a webpage

Posters on stackoverflow may have suggestions for workarounds

关于visual-studio-code - WebView 中的 VSCode 跨源请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55841688/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com