Home > front end >  Alert function in react native
Alert function in react native

Time:06-24

I have set an alert message on the api script but when it successful run on the frontend wont be able to prompt the alert successful message

here my code, this is the frontend code which expected to return the message from the server

fetch(InsertAPIURL, {
        mode:'no-cors',
        method: 'POST',
        headers: headers,
        body: JSON.stringify(Data)
        })
        .then((response) =>response.json())
        .then((responseJson)=>{
        Alert.alert(responseJson);
        }).catch((error) => {
        console.error("Error" error);
        })

here is the backend script code

$register = mysqli_query($CN, $insertuserid);

    if ($register) {
        $Message = "Member has been registered successfully";
        $EncodedData=json_encode($Message);
        echo $EncodedData;
    }
        
    else {
        echo "Server Error... please try latter";

    }

after execute the data successful insert into the server but it wont prompt anything hope u guys help, thanks in advance

CodePudding user response:

Backend Php: you write the following below:

//backend php
if(ok){
 echo json_encode(array("success"=>1,"messsage"=>"successfully"));
}
else{
  echo json_encode(array("success"=>0,"messsage"=>"Error Register"));
}


//frontEnd React
fetch(URL,options).then(res=>res.json()).then(result=>{
  if(result.success===1){
    //write code successfully
    Alert.
  }
  else{
   //write code error register
    Alert.
  }
});

CodePudding user response:

Alert Doc: https://reactnative.dev/docs/alert#alert

static alert(title, meesage?, buttons?, options?)

Parameters:

NAME TYPE DESCRIPTION
title string The dialog's title. Passing null or empty string will hide the title.
message string An optional message that appears below the dialog's title.
buttons Buttons An optional array containing buttons configuration.
options Options An optional Alert configuration for the Android.

You can only use string, null, and empty type in the title. responseJSON is not valid type.

fetch(InsertAPIURL, {
        mode:'no-cors',
        method: 'POST',
        headers: headers,
        body: JSON.stringify(Data)
        })
        .then((response) =>response.json())
        .then((responseJson)=>{
        Alert.alert(responseJson);
        }).catch((error) => {
        console.error("Error" error);
        })

to

fetch(InsertAPIURL, {
        mode:'no-cors',
        method: 'POST',
        headers: headers,
        body: JSON.stringify(Data)
        })
        .then((response) =>response.json())
        .then((responseJson)=>{
        Alert.alert(JSON.stringify(responseJson));
        }).catch((error) => {
        console.error("Error" error);
        })
  • Related