简体   繁体   English

React Native- JSON解析错误:意外的标识符“no”

[英]React Native- JSON Parse error: Unexpected identifier “no”

  • When I filter the products with respect to category and showing products in ViewProducts.js, I am getting this issue. 当我根据类别过滤产品并在ViewProducts.js中显示产品时,我遇到了这个问题。
  • When I filter the products with respect to category and only showing in the alert in categiry.js it is showing me all the fetched filtered data in the alert box. 当我根据类别过滤产品并仅显示在categiry.js中的警报时,它会在警报框中显示所有提取的过滤数据。
  • Whereas when I am not filtering products with respect to the category, it is showing me all the products with no error. 然而,当我没有过滤关于该类别的产品时,它向我显示所有产品没有错误。

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.

相关问题 React Native JSON 解析错误意外的标识符 - React Native JSON Parse error Unexpected identifier JSON 解析错误:尝试解析数组中的对象时出现意外的标识符“未定义”(React-Native) - JSON Parse error: Unexpected identifier "undefined" when trying to parse an object from an array (React-Native) 在位置1 / Asyncstorage上以JSON响应Native-意外令牌o - React Native- Unexpected Token o in JSON at position 1 / Asyncstorage 获取 JSON Parse 错误:使用 React Native 和 React 导航时出现意外标识符“未定义”? - Getting JSON Parse error: Unexpected identifier "undefined" when using React native & React navigation? JSON 解析错误:意外的标识符“be” - JSON Parse error: Unexpected identifier "be" React Native-错误访问获取的JSON数据对象 - React Native- Error Accessing Fetched JSON data objects SyntaxError:JSON解析错误:意外的标识符“功能” - SyntaxError: JSON Parse error: Unexpected identifier “function” JSON 解析错误:意外的标识符“RCTWebSocketModule” - JSON Parse error: Unexpected identifier "RCTWebSocketModule" 修复 JSON 解析错误:意外的标识符“数组” - Fix JSON Parse error: Unexpected identifier "array" JSON 解析错误:带有 localStorage 的意外标识符“未定义” - JSON Parse error: Unexpected identifier "undefined" with localStorage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM