1

I'm working on a registration system using vue & php, on my backend, I want error to stored and return to vue if no error, I want the vue to alert "registration successful".

I got the re-routing right but how do extract the errors from my api response,

in php i have


if(count($error)){
echo $error;
}
 
else {
echo json_encode(["isRegistered"=>true]);

}

in vue I have

data(){
return{
...
data : {// user data goes here},
response : ""
},
}

methods: {
...
axio.post("api/register.php"  data, config)
.then((response) =>{this.response = response.data); 
//the problem
console.log(this.response);
}) ;

}


In only getting Array in my console except when i change

echo $error

to

print_r($error)

in php script

opeolluwa
  • 96
  • 8
  • You can't access asynchronously set data outside a callback. See https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – Estus Flask Sep 21 '21 at 06:21

1 Answers1

1

You should return the errors as JSON to make it consistent (and accessible)...

echo json_encode(['errors' => $error]);

You will then at least have access to the errors and you can decide how to display them.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55