首页 > 解决方案 > 可以在 VS Code Webview API 中使用 cookie 吗?

问题描述

我正在尝试使用 Webview API 在 VS Code 中集成一个 Web 应用程序。我有这种错误,提到无法读取 cookie:

error: any
:
DOMException: Failed to read the 'cookie' property from 'Document': Cookies are disabled inside 'data:' URLs. at HttpXsrfCookieExtractor.push../node_modules/@angular/common/fesm5/http.js.HttpXsrfCookieExtractor.getToken (http://127.0.0.1:8585/vendor.js:7688:37) at ApiXsrfInterceptor.push../src/app/lib/atlasmap-data-mapper/services/api-xsrf-interceptor.service.ts.ApiXsrfInterceptor.intercept (http://127.0.0.1:8585/main.js:8780:45) at HttpInterceptorHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptorHandler.handle (http://127.0.0.1:8585/vendor.js:7129:33) at HttpXsrfInterceptor.push../node_modules/@angular/common/fesm5/http.js.HttpXsrfInterceptor.intercept (http://127.0.0.1:8585/vendor.js:7720:25) at HttpInterceptorHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptorHandler.handle (http://127.0.0.1:8585/vendor.js:7129:33) at HttpInterceptingHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptingHandler.handle (http://127.0.0.1:8585/vendor.js:7764:27) at MergeMapSubscriber.project (http://127.0.0.1:8585/vendor.js:6969:184) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (http://127.0.0.1:8585/vendor.js:106781:27) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (http://127.0.0.1:8585/vendor.js:106771:18) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (http://127.0.0.1:8585/vendor.js:101228:18)
code
:
18
message
:
"Failed to read the 'cookie' property from 'Document': Cookies are disabled inside 'data:' URLs."
name
:
"SecurityError"
stack
:
"Error: Failed to read the 'cookie' property from 'Document': Cookies are disabled inside 'data:' URLs.↵    at HttpXsrfCookieExtractor.push../node_modules/@angular/common/fesm5/http.js.HttpXsrfCookieExtractor.getToken (http://127.0.0.1:8585/vendor.js:7688:37)↵    at ApiXsrfInterceptor.push../src/app/lib/atlasmap-data-mapper/services/api-xsrf-interceptor.service.ts.ApiXsrfInterceptor.intercept (http://127.0.0.1:8585/main.js:8780:45)↵    at HttpInterceptorHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptorHandler.handle (http://127.0.0.1:8585/vendor.js:7129:33)↵    at HttpXsrfInterceptor.push../node_modules/@angular/common/fesm5/http.js.HttpXsrfInterceptor.intercept (http://127.0.0.1:8585/vendor.js:7720:25)↵    at HttpInterceptorHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptorHandler.handle (http://127.0.0.1:8585/vendor.js:7129:33)↵    at HttpInterceptingHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptingHandler.handle (http://127.0.0.1:8585/vendor.js:7764:27)↵    at MergeMapSubscriber.project (http://127.0.0.1:8585/vendor.js:6969:184)↵    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (http://127.0.0.1:8585/vendor.js:106781:27)↵    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (http://127.0.0.1:8585/vendor.js:106771:18)↵    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (http://127.0.0.1:8585/vendor.js:101228:18)"

它是 VS Code Webview API 的限制吗?是要配置的吗?

作为参考,我尝试集成的项目是https://github.com/atlasmap/atlasmap,有一个带有“当前状态”的PR(又名不工作)

标签: visual-studio-codevscode-extensions

解决方案


VSCode webview API 也禁用了对 localstorage 和 cookie 的评估,这就是您出错的原因。

这个问题谈论它https://github.com/Microsoft/vscode/issues/48464


推荐阅读