简体   繁体   English

使用 api 反应应用程序返回 - 混合内容:<> 处的页面已通过 HTTPS 加载,但请求了不安全的资源

[英]React app with api returns - Mixed Content: The page at <> was loaded over HTTPS, but requested an insecure resource

I deployed a React App, through Heroku, that uses the API at https://exchangeratesapi.io/ to convert currency.我通过 Heroku 部署了一个 React 应用程序,它使用https://exchangeratesapi.io/ 的 API来转换货币。 When I deploy to via Heroku, the error message in the HTML is "Error: Failed to fetch" and the Error itself is:当我通过 Heroku 部署到时,HTML 中的错误消息是“错误:无法获取”,错误本身是:

CurrencyUI.js:31 Mixed Content: The page at 'https://arcane-forest-12888.herokuapp.com/currency' was loaded over HTTPS, but requested an insecure resource 'http://api.exchangeratesapi.io/latest?access_key=dff081b889aae502999e19f12aab42e5&symbols=USD,ZAR,GBP,EUR'. CurrencyUI.js:31 混合内容:“https://arcane-forest-12888.herokuapp.com/currency”的页面在 HTTPS 上加载,但请求了不安全的资源“http://api.exchangeratesapi.io/latest ?access_key=dff081b889aae502999e19f12aab42e5&symbols=USD,ZAR,GBP,EUR'。 This request has been blocked;此请求已被阻止; the content must be served over HTTPS.内容必须通过 HTTPS 提供。

I tried implementing https://stackoverflow.com/a/67765239/13848021我尝试实现https://stackoverflow.com/a/67765239/13848021

//there's no way to disable mixed content using javascript but you can add this tag //没有办法使用javascript禁用混合内容,但你可以添加这个标签

< meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"> < meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

//to your HTML to allow mixed content //到您的 HTML 以允许混合内容

But then it returns an undefined object.但随后它返回一个未定义的 object。 The API documentation makes it seem like I can simply change my BASE_URL to from http to https but this also returns an undefined object. API 文档使我看起来可以简单地将我的 BASE_URL 更改为从 http 到 https 但这也返回一个未定义的 ZA8CFDE86311CBD49EB269.

Here is my code:这是我的代码:

import React, { useState, useEffect } from 'react'

import CurrencyInput from './CurrencyInput'


const BASE_URL = `http://api.exchangeratesapi.io/latest?access_key=dff081b889aae502999e19f12aab42e5&symbols=USD,ZAR,GBP,EUR`

const CurrencyUI = () => {
     
    const [error, setError] = useState(null)
    const [isLoaded, setIsLoaded] = useState(false)
    const [currencies, setCurrencies] = useState([])
    
    const [usdCurrency, setUsdCurrency] = useState()
    const [zarCurrency, setZarCurrency] = useState()
    const [gbpCurrency, setGbpCurrency] = useState()
    const [eurCurrency, setEurCurrency] = useState()
    
    const [amount, setAmount] = useState()
    
    const [inAmount, setInAmount] = useState(true)
    const [zarExRate, setZarExRate] = useState(false)
    const [gbpExRate, setGbpExRate] = useState(false)
    const [eurExRate, setEurExRate] = useState(false)

    useEffect(() => {
        fetch(BASE_URL)
            .then(res => res.json())
            .then(data => {
                const currencyUSD = Object.keys(data.rates)[0]
                const currencyZAR = Object.keys(data.rates)[1]
                const currencyGBP = Object.keys(data.rates)[2]
                const currencyEUR = Object.keys(data.rates)[3]
                setIsLoaded(true)
                setCurrencies([...Object.keys(data.rates)])
                setUsdCurrency(data.rates[currencyUSD])
                setZarCurrency(data.rates[currencyZAR])
                setGbpCurrency(data.rates[currencyGBP])
                setEurCurrency(data.rates[currencyEUR])
            })
           
            .catch(error => {
                setIsLoaded(true)
                setError(error)
            })
    }, [])    
   
    function handleFromAmountChange(e) {
        setAmount(e.target.value)
        setInAmount(true)
        setZarExRate(false)
        setGbpExRate(false)
        setEurExRate(false)
    }

    function handleZarAmountChange(e) {
        setAmount(e.target.value)
        setInAmount(false)
        setZarExRate(true)
        setGbpExRate(false)
        setEurExRate(false)
    }

    function handleGbpAmountChange(e) {
        setAmount(e.target.value)
        setInAmount(false)
        setZarExRate(false)
        setGbpExRate(true)
        setEurExRate(false)
    }

    function handleEurAmountChange(e) {
        setAmount(e.target.value)
        setInAmount(false)
        setZarExRate(false)
        setGbpExRate(false)
        setEurExRate(true)
    }

    if (inAmount) {
        fromAmount = amount
        zarAmount = ((amount / usdCurrency) * zarCurrency).toFixed(3)
        gbpAmount = ((amount / usdCurrency) * gbpCurrency).toFixed(3)
        eurAmount = ((amount / usdCurrency) * eurCurrency).toFixed(3)
    } else if (zarExRate) {
        zarAmount = amount
        fromAmount = ((amount * usdCurrency) / zarCurrency).toFixed(3)
        gbpAmount = ((amount * gbpCurrency) / zarCurrency).toFixed(3)
        eurAmount = ((amount * eurCurrency) / zarCurrency).toFixed(3)
    } else if (gbpExRate) {
        gbpAmount = amount
        fromAmount = ((amount * usdCurrency) / gbpCurrency).toFixed(3)
        zarAmount = ((amount * zarCurrency) / gbpCurrency).toFixed(3)
        eurAmount = ((amount * eurCurrency) / gbpCurrency).toFixed(3)
    } else if (eurExRate) {
        eurAmount = amount
        fromAmount = ((amount * usdCurrency) / eurCurrency).toFixed(3)
        gbpAmount = ((amount * gbpCurrency) / eurCurrency).toFixed(3)
        zarAmount = ((amount * zarCurrency) / eurCurrency).toFixed(3)
    }    
    
    if (error) {
        return <div className="error">Error: {error.message}</div>
    } else if (!isLoaded) {
        return (
            <div>
                <h1>Loading...</h1>
            </div>
        )
    } else {
        
        return (
            
            <div className="currency-ui">
               
                <CurrencyInput
                    key={usdCurrency}
                    id={usdCurrency}
                    value={fromAmount}
                    onChangeAmount={handleFromAmountChange}
                    placeholder={currencies[0]}
                />
                <CurrencyInput
                    key={zarCurrency}
                    id={zarCurrency}
                    value={zarAmount}
                    onChangeAmount={handleZarAmountChange}
                    placeholder={currencies[1]}
                />
                <CurrencyInput
                    key={gbpCurrency}
                    id={gbpCurrency}
                    value={gbpAmount}
                    onChangeAmount={handleGbpAmountChange}
                    placeholder={currencies[2]}
                />
                <CurrencyInput
                    key={eurCurrency}
                    id={eurCurrency}
                    value={eurAmount}
                    onChangeAmount={handleEurAmountChange}
                    placeholder={currencies[3]}
                />
            </div>
        )
    }
}

export default CurrencyUI

None of the solutions I on here I have implemented solves my problem.我在这里实施的解决方案都没有解决我的问题。 Any ideas on what I can try?关于我可以尝试什么的任何想法?

Try to change const BASE_URL = http://api.exchangeratesapi.io/latest?access_key=dff081b889aae502999e19f12aab42e5&symbols=USD,ZAR,GBP,EUR尝试更改 const BASE_URL = http://api.exchangeratesapi.io/latest?access_key=dff081b889aae502999e19f12aab42e5&symbols=USD,ZAR,GBP,EUR

To:至:

const BASE_URL = https://api.exchangeratesapi.io/latest?access_key=dff081b889aae502999e19f12aab42e5&symbols=USD,ZAR,GBP,EUR const BASE_URL = https://api.exchangeratesapi.io/latest?access_key=dff081b889aae502999e19f12aab42e5&symbols=USD,ZAR,GBP,EUR

if BASE_URL gives you the same response it will work.如果 BASE_URL 给您相同的响应,它将起作用。

I had the same issue, I did change my api to https but unfortunately it is failing as it won't let me fetch data through https我有同样的问题,我确实将我的 api 更改为 https 但不幸的是它失败了,因为它不允许我通过 https 获取数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 页面是通过 https 加载的,但请求了一个不安全的 xmlhttprequest 端点 - The page at was loaded over https but requested an insecure xmlhttprequest endpoint 该页面是通过 HTTPS 加载的,但请求了一个不安全的 XMLHttpRequest 端点 - The page was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 页面通过 HTTPS 加载,但请求了不安全的 XMLHttpRequest 端点 - Page loaded over HTTPS but requested an insecure XMLHttpRequest endpoint 页面已通过HTTPS加载,但请求了不安全的信息。 我找不到它 - Page loaded over HTTPS but requested an insecure. I cannot find it “网站”页面已通过 HTTPS 加载,但请求了不安全的 XMLHttpRequest 端点“网站” - The page at 'website' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'website' 已通过HTTPS加载,但请求了不安全的XMLHttpRequest端点 - was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 混合内容页面:请求不安全的样式表错误 - Mixed Content Page: requested an insecure stylesheet error “https://example.com”上的页面已通过 HTTPS 加载,但请求了不安全的 XMLHttpRequest 端点 - The page at 'https://example.com' was loaded over HTTPS, but requested an insecure XMLHttpRequest Endpoint Heroku错误:页面是通过HTTPS加载的,但是请求了一个不安全的脚本&#39;angular-route.js&#39; - Heroku error: page was loaded over HTTPS, but requested an insecure script 'angular-route.js' HTTPS页面上的不安全内容 - Insecure content on HTTPS page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM