Estou tentando enviar dados de cadastro do meu app em React-native para a minha api em PHP.
Segue meu código 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);
}
}, []);
Meu arquivo api.ts:
import axios from 'axios';
const api = axios.create({
baseURL: 'http://localhost:8888/Projects/Web/SmartTrafficDashboard/api',
});
export default api;
E aqui, meu 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;
}
E o erro que aparece no console do Metro do React é
[Error: Network Error]
OBS: Fiz teste via postman, na api, e o cadastro funcionou.