【问题标题】:Trying to skip pre-flight OPTIONS using JS locally尝试在本地使用 JS 跳过飞行前选项
【发布时间】:2021-10-18 21:16:57
【问题描述】:

我正在使用 js fetch 发送一个简单的 POST 请求,它被发送到 Bartender Label Software Integration Application 但它无法处理使用以下代码时发送的 OPTIONS 或 CORS Pre flight:

<script>
        const TurkishDelightBrownieButton = document.getElementById('Turkish Delight Brownie');
        TurkishDelightBrownieButton.addEventListener('click', function () {
            fetch('http://localhost:8000/brownie', {
                    method: 'POST',
                    headers: {
                        'content-type': 'application/json',
                    },
                    body: {
                        Allergens: 'Egg, Wheat, Soya',
                        Ingredients: "Sugar, Butter, Cream ,stuff like that",
                        Flavour: "Brownie Flavoured",
                        DatePacked: "12/08/2021",
                        BestBefore: "22/08/2021"
                    }
                    })
                    .then(response => {
                        console.log(response)
                    })
                    .catch(err => {
                        console.log(err)
                    })
        });
    </script>

这一切都在本地完成,所以我正在尝试使请求适合 Bartender 集成 - 主要是 application/json。

如果有人对此有任何建议或不同的方法,我很乐意提供帮助!

【问题讨论】:

    标签: javascript html api cors bartender


    【解决方案1】:

    如果我对问题的理解正确,您的前端是在 8000 以外的端口(后端使用的端口)上本地运行的。因此,您的前端向您的后端发出的请求是cross-origin。此外,为您的请求指定的内容类型 (application/json) 是 non-simple one

    因此,浏览器将发送预检请求;没有办法。

    【讨论】:

      猜你喜欢
      • 2014-02-07
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      相关资源
      最近更新 更多