如何打开IBM代码引擎应用程序的COR

发布于 2025-02-10 20:49:13 字数 1242 浏览 1 评论 0原文

我创建了一个正在暴露几个API的代码引擎应用程序。它的容器是使用云本机构建包构建的,因此我可以解决安全问题的修复程序。

我可以从浏览器和卷发中成功调用API,但是当我尝试从React.js应用中调用API时,我会收到以下错误 -

Access to fetch at '...' from origin 'http://localhost:3000' 
has been blocked by CORS policy: 
Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 

我需要在我的代码引擎应用程序上启用CORS,但是正在努力努力确定如何。

通过运行npx create-react-app创建了react.js应用程序骨架。我正在测试react.js应用本地运行npm start。当准备就绪时,将通过运行npm run build来构建。

对代码引擎应用程序API的其余呼叫一次<代码>使用,其中endpoint and method是对组件的输入:

    useEffect(() => {
        if (!endpoint || !method) return;
        console.log('Starting');

        const appurl = `https://${endpoint}${method}`;

        fetch(appurl, {
            method: 'GET',
            headers: {
                "Content-Type": "application/json" 
            }
        })
        //.then(response => response.json())
        .then(response => console.log(response))
        .catch(console.error);

        console.log('url is ', appurl);
    },[]);

I have created a Code Engine application which is exposing a couple of APIs. Its container is built using a Cloud Native Buildpack, so I can pick up fixes to security issues.

I can successfully invoke the APIs from a browser and from curl, but when I attempt to invoke the APIs from a React.js app, I get the following error -

Access to fetch at '...' from origin 'http://localhost:3000' 
has been blocked by CORS policy: 
Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 

I need to enable cors on my code-engine application, but am struggling to determine how.

The react.js app skeleton was created by running npx create-react-app. I am testing the react.js app locally running npm start. When it is ready it will be built by running npm run build.

The REST call to the Code Engine Application API is in a fire one time useEffect, where endpoint and method are inputs to the component:

    useEffect(() => {
        if (!endpoint || !method) return;
        console.log('Starting');

        const appurl = `https://${endpoint}${method}`;

        fetch(appurl, {
            method: 'GET',
            headers: {
                "Content-Type": "application/json" 
            }
        })
        //.then(response => response.json())
        .then(response => console.log(response))
        .catch(console.error);

        console.log('url is ', appurl);
    },[]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心舞飞扬 2025-02-17 20:49:13

对于后CORS请求,您的应用程序必须服务于跨越前请求的选项。也许是问题?请参阅 https://developer.mozilla.orgla.orgg/en-us/docs/docs/docs/docs/en-us/docs /Web/HTTP/CORS 或CORS上的其他资源。特别是对于发布请求,您需要提供访问控制范围的选项。

因为带有API的代码引擎应用程序由Node.js/ Express供电,而所有需要完成的是添加CORS 作为依赖项,然后添加

const cors = require('cors');

app.use(cors());

参见 https://expressjs.com/en/resources/middleware/cors.html

For POST CORS requests, your app has to serve the OPTIONS preflight request. Maybe that’s the issue? See https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS or other resources on CORS. For POST requests in particular, you need to provide Access-Control-Allow-Headers options.

Because the Code Engine app with the API is powered by Node.js / Express, and all that needs to be done is to add cors as a dependency, then add

const cors = require('cors');

app.use(cors());

See https://expressjs.com/en/resources/middleware/cors.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文