React App:EventSource 的响应具有 MIME 类型(“application/json”),而不是“text/event-stream”。中止连接

Leo*_*ban 6 javascript http-headers reactjs axios

重构我的应用程序以使用新的价格 API,但出现以下错误:

EventSource 的响应具有 MIME 类型(“application/json”),而不是“text/event-stream”。正在中止连接。

http://docs.nomics.com/#tag/Currency的免费 API 文档/currencies/ticker

这是我添加后的代码'Content-type': 'text/event-stream'

const headers: IHeaders = {
  baseURL: NOMICS_API_BASE_URL, // 'https://api.nomics.com/v1/'
  headers: {
    'Content-Type': 'text/event-stream'
  },
  params: {
    key: NOMICS_KEY
  }
}

// * GET Currencies
export const getCurrenciesRequest = async () => {
  console.log('getCurrenciesRequest...')
  const nomics = axios.create(headers)

  try {
    const currencies = await nomics.get(`currencies/ticker&ids=BTC,ETH,XRP&interval=1d,30d&convert=USD`)
    console.log('currencies', currencies)
    return currencies
  } catch (err) {
    return err
  }
}
Run Code Online (Sandbox Code Playgroud)

也刚刚尝试过

const currencies = await axios.get(`https://api.nomics.com/v1/currencies/ticker?key=demo-26240835858194712a4f8cc0dc635c7a&ids=BTC,ETH,XRP&interval=1d,30d&convert=USD`)
Run Code Online (Sandbox Code Playgroud)

和小写键 'content-type': 'text/event-stream'

不知道我错过了什么,希望能在这里得到一些想法......

更新

我现在可以通过删除来获取响应axios.create(headers)

export const getCurrenciesRequest = async () => {
  console.log('getCurrenciesRequest...')

  try {
    const currencies = await axios.get(`https://api.nomics.com/v1/currencies/ticker?key=demo-26240835858194712a4f8cc0dc635c7a&ids=BTC,ETH,XRP&interval=1d,30d&convert=USD`)
    console.log('currencies', currencies)
    return currencies
  } catch (err) {
    return err
  }
}
Run Code Online (Sandbox Code Playgroud)

但是我仍然遇到同样的错误

EventSource 的响应具有 MIME 类型(“application/json”),而不是“text/event-stream”。正在中止连接。

在此输入图像描述

小智 8

您是否使用 CORS 扩展?我也遇到了这个问题,并遇到了这篇文章:SSE `this.eventSource.onmessage` 调用失败。错误“EventSource 的响应的 MIME 类型(“application/json”)不是“text/event-stream”

您可能需要禁用 CORS 扩展才能消除该控制台错误。