POST失败,无法将ReadableNativeMap强制转换为String错误 [英] POST fails with ReadableNativeMap cannot be cast to String error

查看:71
本文介绍了POST失败,无法将ReadableNativeMap强制转换为String错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 React Native 中工作,我在后端使用 PHP ,而当我使用访存 POST 请求时,我得到了一个非常奇怪的错误,我不知道为什么会这样.我检查了网址,所以它没有问题,正常的 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接收到一个字符串,并且从访存响应中获得的是内部的 com.facebook.react.bridge.ReadableNativeMap 对象(这是获取的本机实现返回的内容).您可以尝试:

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强制转换为String错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆