I am a developing a React Native app and fetching API for login.Below is my store where I an calling API.
auth.js:
import AsyncStorage from '@react-native-community/async-storage';
export const LOGIN ='LOGIN';
export const login=(textemailid,textpassword) =>{
formData.append('txtUemail',textemailid);
formData.append('txtUpass',textpassword);
return async dispatch =>{
await fetch('https://----------------------/login.php',
{
method:'post',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: formData
})
.then(res => res.text())
.then(
(loginresult) =>{
console.log(loginresult);
}).catch(err =>{
console.log(err);
})
const saveDataToStorage = (loginresult) =>{
AsyncStorage.setItem('userData',JSON.stringify({
loginresult:loginresult
}))
}
dispatch({type:LOGIN});
}
}
I am getting the output in console as in below screen.It gives some spaces and then success.
If I parse the result using json like .then(res => res.json()) it gives
JSON Parse error: Unexpected identifier "success".So I am parsing as .then(res => res.text()) .
In php program I am echoing string as given below:
if($check == true){
echo "success";
}
else
{
echo "Login Failed";
}
I want string so that I can allow user to login.I dont know why I am getting escape characters in the result.Can anyone tell me where I am going wrong.Thanks in Advance.
source https://stackoverflow.com/questions/69743628/react-native-fetch-api-returning-text-with-escape-characters
Comments
Post a Comment