外部api上任何前缀的CORS都需要400标头

问题描述

我正在使用React,并且想要显示来自外部api的图像,并且在api的任何位置都给它加了CORS前缀。但我收到此错误消息:

GET https://cors-anywhere.herokuapp.com/https://api.fortnox.se/3/archive/8c05c536-c110-402d-82da-60f25f6b0e1c 400 (Header required)

我一直在寻找解决方案,但是我只找到有关如何在服务器端解决此问题的解决方案。但是由于这是一个外部api,所以我不能这样做。有什么解决办法吗?

这是我的代码

export const SingleProductimage = (props) => {
  const [articleImg,setArticleImg] = useState('')
  console.log(articleImg)
  console.log(props.fileid)

  useEffect(() => {
    fetch(`https://cors-anywhere.herokuapp.com/https://api.fortnox.se/3/archive/${props.fileid}`,{
      method: 'GET',headers: {
        'Content-Type': 'application/json',Accept: 'application/json','Access-Token': accesstoken,'Client-Secret': clientSecret,}
    })

      .then((res) => {
        setArticleImg(res.url)
      
      })
      .catch((err) => console.log(err))
  },[props.fileid])

  return (
    <div>
      <img src={articleImg} alt="product" className="img" />
    </div>
  )
}

谢谢!

解决方法

您似乎要尝试代理的URL当前无法通过cors-anywhere.herokuapp.com访问

此处有更多信息:https://github.com/Rob--W/cors-anywhere/issues/39

,

实际上,您正在尝试获取合并的URL https://cors-anywhere.herokuapp.com/https://api.fortnox.se/3/archive/8c05c536-c110-402d-82da-60f25f6b0e1c ,这基本上是 https://cors-anywhere.herokuapp.com/ https://api.fortnox.se/3/archive/8c05c536-c110-402d-82da-60f25f6b0e1c

您必须获取 https://api.fortnox.se/3/archive/8c05c536-c110-402d-82da-60f25f6b0e1c API,然后继续使用访问令牌