You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native应用运行报错:value for message cannot be cast from ReadableNativeMap to string

问题解决:React Native报错 value for message cannot be cast from ReadableNativeMap to string

核心问题及修复步骤

1. React Native端Alert参数类型不匹配

这个报错直接来自catch块里的Alert.alert("Error", error)——React Native的Alert.alert要求第二个参数必须是字符串,但fetch抛出的error是一个ReadableNativeMap对象,直接传就会触发类型转换错误。

修改catch块代码:

.catch(error=>{
    // 把错误转为可读的字符串
    const errorText = error.message || JSON.stringify(error);
    Alert.alert("Error", errorText);
});

2. PHP端语法错误(关键隐性问题)

你的PHP代码里$_GET("json")是错误写法,$_GET是超全局数组,访问元素必须用方括号$_GET['json']。这个错误会导致PHP返回错误页面而非预期的JSON,前端response.json()解析失败就会进入catch块,间接触发上面的Alert报错。

修复后的PHP代码:

<?php

$json2 = $_GET['json']; // 替换括号为方括号
$obj2 = json_decode($json2);

$obj1 = new stdClass();
$obj1->name = "shavi";
$obj1->gender = "female";
$obj1->mobile = "0713456789";
$obj1->x = $obj2->x;
$obj1->y = $obj2->y;

$json1 = json_encode($obj1);
echo $json1;

?>

额外优化建议

  • GET请求传JSON时记得URL编码,避免特殊字符破坏参数:
    const encodedJson = encodeURIComponent(json2);
    const request = fetch(`http://10.0.2.2/react_php?json=${encodedJson}`, requestObject);
    
  • 前端先校验响应状态码,提前捕获HTTP错误:
    request.then(response => {
        if (!response.ok) {
            throw new Error(`请求失败,状态码:${response.status}`);
        }
        return response.json();
    })
    

内容的提问来源于stack exchange,提问作者Wasantha Sanjaya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 21:32:59