简体   繁体   English

如何在 fetch() 中使用 no-cors

[英]How use no-cors with fetch()

I have a problem.我有个问题。 I try to explain it.我试着解释一下。

My client ask me to build a web portal for fetching and modifying data on private server, with Basic Auth.我的客户要求我构建一个 web 门户,用于在私有服务器上获取和修改数据,使用基本身份验证。

The private server return a XML files.私有服务器返回一个 XML 文件。

I try using fetch() to get the data but I have a CORS error.我尝试使用 fetch() 获取数据,但出现 CORS 错误。 I know what that mean but I don't have the hand on the private server.我知道那是什么意思,但我没有私人服务器上的手。

So I would like use my fetch() with:所以我想将我的 fetch() 用于:

mode : 'no-cors'

but if I use like that, I can use the data fetched.但如果我这样使用,我可以使用获取的数据。

I think I have 2 main solutions:我想我有两个主要的解决方案:

  • Add CORS support to the API you are using.将 CORS 支持添加到您正在使用的 API 中。 This only works if you have control over the target.这仅在您可以控制目标时才有效。
  • Instead of making the request from your domain, something else needs to make the request for you.不是从您的域发出请求,而是需要其他东西为您发出请求。

If I can't add CORS headers, I will likely want to build a small server-side script that can make these requests on my behalf.如果我无法添加 CORS 标头,我可能想要构建一个小型服务器端脚本,它可以代表我发出这些请求。

Instead of calling the target directly, my script can now call my script, which has to do the request for you server-side.我的脚本现在可以调用我的脚本,而不是直接调用目标,它必须在服务器端为您执行请求。

But, if I don't have the hands on the server, how can I do that?但是,如果我没有在服务器上的手,我该怎么做呢?

If someone have an idea...如果有人有想法...

Thanks a lot...非常感谢...

---- EDIT ----- - - 编辑 - - -

My code:我的代码:

getListApps: async function () {

            let url = `${SA_BASE_URL}/applications`;

            // Set headers
            let headers = new Headers();
            headers.append('Authorization', 'Basic ' + btoa(SA_LOGIN + ":" + SA_PASSWORD));
            try {
                // GET request
                const response = await fetch(url, { 
                    method: 'GET', 
                    headers: headers, 
                    mode: 'no-cors', 
                    credentials: 'include' })

                if (response.status === 200) {
                    const data = await response.json();
                    this.listApp = data;
                    this.listApp.forEach(app => {
                        if (app.status === "DISCONNECTED") {
                            this.listDecApp.push(app);
                        }
                    });
                    this.nbr = this.listDecApp.length;
                } else {
                    if (response.status === 400) this.errors = ['Invalid app_permissions value. (err.400)'];
                    if (response.status === 401) this.errors = ['Acces denied. (err.401)'];
                }
            } catch (error) {
                console.log(error);
                this.errors = ["Une erreur est survenue lors de la récupération des informations sur le serveur."]
            }
            
        },

If you can use no-cors, set it in the headers , like:如果您可以使用 no-cors,请将其设置在headers中,例如:

var opts = {
  headers: {
    'mode':'cors'
  }
}
fetch(url, opts)

If you do not control the API, there is little you can do.如果您不控制 API,您将无能为力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM