POST fails with ReadableNativeMap cannot be cast to String error

人走茶凉 提交于 2021-02-07 09:20:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!