获取请求没有 return 请求的数据

Fetch request doens't return the requested data

我正在尝试使用 fetch() 请求和适当的 header 获取 XML 数据。 请求成功但响应为空。奇怪的是,调试器中的浏览器网络选项卡给我一个 200 OK 并具有请求的响应有效负载,但我似乎无法获取此数据,即使它存在。关于原因的任何线索?

Snapshot of the debugger

data = {
            get:(url)=>{
                return new Promise(resolve => {
                    let auth = new Headers({
                        "username":key.user,
                        "password":key.password,
                        "Accept":"application/xml"                 
                    })
                    
                    fetch(url,{
                        method:"get",
                        mode:"no-cors",
                        headers: auth,
                        credentials:"include"
                    })
                    .then(response => console.log(response))
                    //empty response
                })
            }
        }

您正在发出跨源请求,但您说 mode:"no-cors",

mode:"no-cors", 表示 "If I need permission to do something cross-origin, then don't ask for it, and don't throw an error".

no-cors — Prevents the method from being anything other than HEAD, GET or POST, and the headers from being anything other than simple headers. If any ServiceWorkers intercept these requests, they may not add or override any headers except for those that are simple headers. In addition, JavaScript may not access any properties of the resulting Response. This ensures that ServiceWorkers do not affect the semantics of the Web and prevents security and privacy issues arising from leaking data across domains.

由于您需要 权限才能从跨源请求中读取数据,它会静默失败(因为您明确告诉它这样做)。

使用mode: "cors"