-1
I’m developing an app and I need to log in. I have an API on two different servers.
a return me:
Possible Unhandled Promise Rejection (id: 0): SyntaxError: JSON Parse error: Unrecognized token '<'
and the other:
Possible Unhandled Promise Rejection (id: 0):
SyntaxError: JSON Parse error: Unexpected EOF
Login screen:
const handleSignClick = async () => {
if(cpfField != '' && passworldField != ''){
let json = await Api.signIn(cpfField, passworldField);
if(json){
alert("DEU CERTO");
} else {
alert("CPF e/ou senha errados!");
}
} else {
alert("Preencha os campos!");
}
}
Calling for:
signIn: async (cpf, pass) => {
const req = await fetch(`${BASE_API}/login/index.php`, {
method: 'POST',
headers:{
'Accept': 'application/json',
'Content-Type':'application/json'
},
body: JSON.stringify({cpf, pass})
});
const json = await req.json();
console.log("CPF: ", cpf);
console.log("Pass: ", pass);
console.log("Json: ", json);
return json;
}
API:
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, PUT, DELETE, OPTIONS");
header("Content-Type: application/json");
include 'conexao.php';
$method = strtolower($_SERVER['REQUEST_METHOD']);
if($method === 'post') {
$array = array('error' =>'');
$data = json_decode(file_get_contents('php://input'));
if(is_null($data)){
$data = $_POST;
}
$cpf = $data["cpf"];
$pass = $data["pass"];
// Verifico os dados e retorno
header("Content-Type: application/json");
echo json_encode($array);