问题
I'm working in React Native
, I use PHP
for backend and when I use fetch POST
request I get so strange error, and I dont know why it happens. I checked the url so it works no problem, also normal fetch()
is working without POST
but when I try to post it happens. When I try it in local server fetch POST
works.. but in server, I get this error :
ERROR : com.facebook.react.bridge.ReadableNativeMap cannot be cast to java.lang.String
React native codes :
fetch('http://xxx/react_test1', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: inputName,
email: inputEmail,
phone: inputPhone
}),
}).then((response) => response.json())
.then((responseJson) => {
Alert.alert(responseJson);
}).catch((error) => {
alert(error);
});
回答1:
Alert.alert receives an string, and what you're getting from the fetch response is internally a com.facebook.react.bridge.ReadableNativeMap
object (which is what the native implementation for fetch returns).
You can try:
Alert.alert(JSON.stringify(responseJson))
If you were using iOS you'll get a completely different error:
Exception '-[_NSFrozenDictionaryM length]: unrecognized selector ...
回答2:
Alert.alert only accepts string as input.
Use alert()
instead to show the popup.
Example: alert("Response: ", responseJson)
Happy Coding. :)
来源:https://stackoverflow.com/questions/49971610/post-fails-with-readablenativemap-cannot-be-cast-to-string-error