【问题标题】:React App: EventSource's response has a MIME type ("application/json") that is not "text/event-stream". Aborting the connectionReact App:EventSource 的响应具有不是“text/event-stream”的 MIME 类型(“application/json”)。中止连接
【发布时间】:2020-08-29 03:53:27
【问题描述】:

重构我的应用以使用新的价格 API,我收到以下错误:

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

/currencies/tickerhttp://docs.nomics.com/#tag/Currencies 的免费 API 文档

这是我添加'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
  }
}

也试过了

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

和小写的键 '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
  }
}

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

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

【问题讨论】:

  • 您是否尝试跳过 Content-Type 标头?
  • @Rayon 是的,当我没有 Content-Type 或 headers 对象时,我遇到了这个错误。
  • const currencies = await axios.get(api.nomics.com/v1/currencies/…) 的结果是什么。我试过了,它奏效了。我猜其他一些全局配置正在影响/破坏这一点。
  • 您是否还有其他网络请求正在使用EventSource
  • 快乐编码 ;)

标签: javascript reactjs axios http-headers


【解决方案1】:

您使用的是 CORS 扩展程序吗?我也遇到了这个问题,并遇到了这个帖子:SSE `this.eventSource.onmessage` call fails. Error `"EventSource's response has a MIME type ("application/json") that is not "text/event-stream"

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2018-01-31
    • 2017-05-21
    • 1970-01-01
    相关资源
    最近更新 更多