JSON 解析错误:无法识别的令牌'<' - react-native

新手上路,请多包涵

“JSON 解析错误:无法识别的令牌’<‘”点击 api 时显示错误。代码附在下面注*:响应为 JSON 格式。

 fetch("http:/example.com", {method: "POST",
  body: JSON.stringify(
    {
      uname: uname,
      password: password
    }
  )
})
.then((response) => response.json())
.then((responseData) => {
  AlertIOS.alert(
      "POST Response",
      "Response Body -> " + JSON.stringify(responseData.body)
  )
}).done();
       this.props.navigation.navigate("Home")
   };

请帮忙。谢谢,

原文由 Ananta Prasad 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 955
2 个回答

最后下面的代码工作了。问题在于身体参数。

 fetch("http:/example.com", {method: "POST",
  body: "uname=value1&password=value2" // <-- Post parameters
})
.then((responseData) => {
  AlertIOS.alert(
      "POST Response",
      "Response Body -> " + JSON.stringify(responseData.body)
  )
}).done();
       this.props.navigation.navigate("Home")
};

原文由 Ananta Prasad 发布,翻译遵循 CC BY-SA 4.0 许可协议

这意味着您从服务器获得的 Html 响应可能是 404 或 500 错误。而不是 response.json() 使用 response.text() 您将获得文本中的 html。

 fetch("http:/example.com", {method: "POST",
  body: JSON.stringify(
    {
      uname: uname,
      password: password
    }
  )
})
.then((response) => response.text())
.then((responseData) => {
  AlertIOS.alert(
      "POST Response",
      "Response Body -> " + responseData
  )
}).done();
       this.props.navigation.navigate("Home")
   };

原文由 ispirett 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题