簡體   English   中英

React Native- JSON解析錯誤:意外的標識符“no”

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

  • 當我根據類別過濾產品並在ViewProducts.js中顯示產品時,我遇到了這個問題。
  • 當我根據類別過濾產品並僅顯示在categiry.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);
    }); 
}

在此輸入圖像描述

如果您遇到錯誤且不是json_encoded,通常會發生這種情況。 嘗試捕獲您收到的所有錯誤並回顯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;
}

否則你可以共享你的服務器代碼,將有助於排除故障!

這是因為您獲得的響應不是正確的JSON格式,因此在將其克隆到列表視圖之前,請執行console.log或console.warn的fetch(URL)然后(response)=> response.text()並在此處打印然后,如果你正在獲得JSON意味着解析那個或者顯示一些零食 在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM