Trying to send data to the api in React Native and php

Asked

Viewed 73 times

1

I’m trying to send my app registration data in React-Turn to my PHP api.

Follow my code React:

const handleSignUp = useCallback(async (data: object) => {
try {
  await signUpValidation.validate(data, {
    abortEarly: false,
  });
  const result = await api.post('/client.php', {
    ...data,
    option: "register" 
  });
  console.log(data)
  console.log(result);

 } catch (error) {
   if (error instanceof Yup.ValidationError) {
     const validationErrors = getValidationErrors(error);
     formRef.current?.setErrors(validationErrors);
  }
  console.log(error);
 }
}, []);

My api.ts file:

import axios from 'axios';

const api = axios.create({
   baseURL: 'http://localhost:8888/Projects/Web/SmartTrafficDashboard/api',
});

export default api;

And here, my php:

<?php
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
ini_set('display_errors', true);
error_reporting(E_ALL);

include_once("con.php");

$pdo = conectar();

$data = file_get_contents("php://input");
$data = json_decode($data);

if($data){
    $option = $data->option;
}else{
    $option = $_GET['option'];
}

switch ($option) {
    case 'register':
        $name = $data->clientName;
        $rg = $data->rg;
        $cpf = $data->cpf;
        $cnh = $data->cnh;
        $email = $data->email;
        $password = md5($data->password);

        $carBrand = $data->carBrand;
        $carModel = $data->carModel;
        $licensePlate = $data->licensePlate;

        $registerClient=$pdo->prepare("INSERT INTO client (idclient, name, rg, cpf, cnh, email, password) VALUES(?,?,?,?,?,?,?)");
        $registerClient->bindValue(1, NULL);
        $registerClient->bindValue(2, $name);
        $registerClient->bindValue(3, $rg);
        $registerClient->bindValue(4, $cpf);
        $registerClient->bindValue(5, $cnh);
        $registerClient->bindValue(6, $email);
        $registerClient->bindValue(7, $password);
        $registerClient->execute();

        $idclient = $pdo->lastInsertId();

        $registerVehicle=$pdo->prepare("INSERT INTO clientvehicle (idclientvehicle, idclient, brand, model, licenseplate) VALUES(?,?,?,?,?)");
        $registerVehicle->bindValue(1, NULL);
        $registerVehicle->bindValue(2, $idclient);
        $registerVehicle->bindValue(3, $carBrand);
        $registerVehicle->bindValue(4, $carModel);
        $registerVehicle->bindValue(5, $licensePlate);
        $registerVehicle->execute();

        break;

    default:
        # code...
        break;
}

And the error that appears on the React Metro console is

[Error: Network Error]

NOTE: I tested via Postman, in api, and the registration worked.

  • 1

    Put fixed ip and try by ip not as localhost, it worked for me

1 answer

1

Hello, if you are using the emulator of Android Studio tries to change the localhost for 10.0.2.2 as far as I know the emulator of Android studio does not understand the localhost

  • It would be interesting to explain why you don’t understand localhost... and also why 10.0.2.2?

  • Hello friend sorry, I don’t know why, I had this problem a while ago, so when I saw your question I remembered that I had already passed so that’s how it worked.

Browser other questions tagged

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