代码之家  ›  专栏  ›  技术社区  ›  Leon Gaban

React App:EventSource的响应有一个MIME类型(“application/json”),不是“文本/事件流”。中止连接

  •  0
  • Leon Gaban  · 技术社区  · 4 年前

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

    EventSource的响应具有不是“文本/事件流”的MIME类型(“application/json”)。正在中止连接。

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

    这是我添加后的代码 '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的响应具有不是“文本/事件流”的MIME类型(“application/json”)。正在中止连接。

    enter image description here

    0 回复  |  直到 4 年前
        1
  •  8
  •   Dharman Aman Gojariya    4 年前

    您是否正在使用CORS扩展?我也遇到了这个问题,并浏览了这篇文章: SSE `this.eventSource.onmessage` call fails. Error `"EventSource's response has a MIME type ("application/json") that is not "text/event-stream"

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