简体   繁体   English

在反应中从 Api 获取数据

[英]Fetch data from Api in react

I want to fetch the data from this api (refer the screenshots) in reactjs using fetch function..I tried different ways but none of them is working我想使用 fetch function 从 reactjs 中的这个 api(参考屏幕截图)中获取数据。我尝试了不同的方法,但都没有用

I am doing this way我这样做

useEffect(() => {
    // POST request using fetch inside useEffect React hook
    const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ "firebase_id":"fonnnnjnvQXzyOcXK5kW88PM8jnjnhnhb1"})
    };
    fetch('demo.revon.com/audi/zero/li', requestOptions)
        .then(response => response.json())
        .then(data =>  console.log(data));                 

You should try to call your API without https://localhost:3000 in front of your API link.您应该尝试在您的 API 链接前面不使用 https://localhost:3000 来调用您的 API。

You forgot to put HTTP or HTTPS so it consider a subfolder of localhost:3000.您忘记输入 HTTP 或 HTTPS,所以它认为是 localhost:3000 的子文件夹。

You must do something like that: (or http if not https)您必须执行类似的操作:(如果不是 https,则为 http)

fetch('https://demo.revon.com/audi/zero/li', requestOptions)
        .then(response => response.json())
        .then(data =>  console.log(data));    

Regards问候

You'll need to mention https:// in the fetch url您需要在提取 url 中提及 https://

requestOptions is not the issue here! requestOptions不是这里的问题!


You are OBVIOUSLY hitting the localhost domain, not demo.revon .您显然是在访问localhost域,而不是demo.revon
Insert the full URL: https://demo.revon.com/audi/zero/li插入完整的 URL: https://demo.revon.com/audi/zero/li

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM