首页 > 解决方案 > 使用 fetch 进行 api 调用时无法授权

问题描述

反应代码以获取 api 数据

    getData = () => {
        const url = FormatUrl(`/cluster`)
        fetch(url,  { 
        method: 'GET', 
        headers: new Headers({
            'Authorization': 'Basic my_token', 
            'Content-Type': 'application/x-www-form-urlencoded'
    })})
        .then(res => res.json())
        .then(res => {
            console.log(res, "=")
        })
    }

错误:

    Access to fetch at 'https://beta.cnox.io:3001/cluster' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.
    beta.cnox.io:3001/cluster:1 Failed to load resource: net::ERR_FAILED

在这里,我正在尝试从上面的 api 获取数据并发送令牌,但得到以下错误。相同的 api 是从角度工作的。请看一下

标签: reactjs

解决方案


推荐阅读