POST 失败,ReadableNativeMap 无法转换为字符串错误 [英] POST fails with ReadableNativeMap cannot be cast to String error
问题描述
我在 React Native
中工作,我使用 PHP
作为后端,当我使用 fetch POST
请求时,我得到了很奇怪的错误,我不知道为什么会这样.我检查了 url,所以它没有问题,正常的 fetch()
也可以在没有 POST
的情况下工作,但是当我尝试发布它时会发生.当我在本地服务器中尝试获取 POST
工作时.. 但在服务器中,我收到此错误:
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 :
错误:com.facebook.react.bridge.ReadableNativeMap 无法转换为java.lang.String
ERROR : com.facebook.react.bridge.ReadableNativeMap cannot be cast to java.lang.String
反应原生代码:
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);
});
推荐答案
Alert.alert 接收到一个字符串,而您从 fetch 响应中得到的内部是一个 com.facebook.react.bridge.ReadableNativeMap
对象(这是 fetch 的本机实现返回的内容).你可以试试:
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))
如果你使用的是 iOS,你会得到一个完全不同的错误:
If you were using iOS you'll get a completely different error:
Exception '-[_NSFrozenDictionaryM length]: unrecognized selector ...
这篇关于POST 失败,ReadableNativeMap 无法转换为字符串错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!