Zaid Khan
Zaid Khan

Reputation: 54

React Native- JSON Parse error: Unexpected identifier "no"

Below is the code for fething in ViewProducts.js

  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);
    }); 
}

enter image description here

Upvotes: 3

Views: 5878

Answers (2)

keerthi c
keerthi c

Reputation: 71

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 enter image description here

Upvotes: 0

Magige Daniel
Magige Daniel

Reputation: 1122

This usually happen if you are having an error and its not json_encoded. Try catching all the errors you are receiving and echo Json response For instance mysql insert error could be handled as below

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!

Upvotes: 2

Related Questions