[英]React Native- JSON Parse error: Unexpected identifier “no”
Below is the code for fething in ViewProducts.js 下面是ViewProducts.js中的fething代码
componentDidMount() {
return fetch('http://192.168.0.109/fyp/products.php')
.then((response) => response.json())
.then((responseJson) => {
let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.setState({
isLoading: false,
dataSource: ds.cloneWithRows(responseJson),
}, function() {
});
})
.catch((error) => {
console.error(error);
});
}
This usually happen if you are having an error and its not json_encoded. 如果您遇到错误且不是json_encoded,通常会发生这种情况。 Try catching all the errors you are receiving and echo Json response For instance mysql insert error could be handled as below 尝试捕获您收到的所有错误并回显Json响应例如,mysql插入错误可以按如下方式处理
if ($conn->query($sql) === TRUE) {
// If the record inserted successfully then show the message.
$MSG = "New record created successfully";
// Converting the message into JSON format.
$json = json_encode($MSG);
// Echo the message.
echo $json;
} else {
$errorMsg="Error: " . $sql . "<br>" . $conn->error;
$json = json_encode($errorMsg);
echo $json;
}
Otherwise you can share your server code, will help troubleshoot! 否则你可以共享你的服务器代码,将有助于排除故障!
This is because the response you are getting is not in the proper JSON format, so before cloning it to list view do console.log or console.warn of fetch(URL)then(response)=>response.text() and print here then if you are getting JSON means then parse that or else show some snack 这是因为您获得的响应不是正确的JSON格式,因此在将其克隆到列表视图之前,请执行console.log或console.warn的fetch(URL)然后(response)=> response.text()并在此处打印然后,如果你正在获得JSON意味着解析那个或者显示一些零食
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.