Axios can’t get data from my PHP server

Asked

Viewed 27 times

0

I’m trying to make the connection between my php server and Axios . I had already used Axios but with Node.js. I’m trying to get him to pull the database data and print it on the screen, but when I put it on my phone he prints it:

inserir a descrição da imagem aqui

and is also shown the following message:

inserir a descrição da imagem aqui

but when I access in my browser it can show the data in the database:

inserir a descrição da imagem aqui

Can anyone help me understand what’s going on? Thank you!

api.js:

import axios from 'axios';
const api = axios.create({
    baseURL : 'http://localhost:3333',
});

export default api;

index js.:

import React ,{useState , useEffect}from 'react';
import { View , Image , TouchableOpacity ,Text , TextInput} from 'react-native';
import api from '../../services/api';

export default function Teste(){
    const [subs , setSubs] = useState([]);
    
    useEffect(()=> {
        api.get('/substancias').then(resp => {setSubs(resp.data)});
    });

    return(
        <View style={{top : 20 , left : 10}}>
            <Text>{subs.length} , {JSON.stringify(api.get('/substancias').then(resp => resp.data))}</Text>
            {subs.map(e => (
            <Text>{e}</Text>
            ))}
        </View>
    )
};
  • 1

    But did not understand why you put in the URL the "localhost", server is not running on mobile (localhost means the device itself), so you have to put the server IP. In the browser worked pq is the same machine that the server is running. In addition, check that the port in question (3333) is accessible by other devices on the network.

  • got it, I was searching but I couldn’t find it , which address should I put then inside the api?? I tried to put IPV4 but msm so n was

  • Probably the 3333 port you used is not open for external requests on your machine, try using port 80, usually it is always available for HTTP communications. One remark, it seems you are quite beginner in development, if it is the case even, I believe that React Native is a very wide step for you now, seek to understand the fundamentals of software development, React and React Native is not for beginners.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.