0

I am trying to retrieve the data from database. Whatever data I retrieve, I put that data into array and then apply json_encode($data). I successfully get Array of Objects in the response as a text. But when I try to JSON.parse(response) I get an error which which says unexpected token [.

First I thought that the data will be already parsed so I tried console.log(response[0]) in order to print the first object, but the result was [. I don't know what's the problem.

My question might be same to others, but I tried all possible solutions, and couldn't solve it through those answers.

My PHP code :

public

function showDeposits($db_conn) {

  $sql = "SELECT * FROM `deposit`";

  if ($db_conn - > query($sql)) {
    $r = $db_conn - > query($sql);
    if ($r - > num_rows > 0) {
      $data = array();
      for ($i = 0; $i < $r - > num_rows; $i++) {
        $data[] = $r - > fetch_assoc();
      }
      echo json_encode($data);
    }
  }

}

My Javascript code :

this.depositForm = function() {
  var form = document.querySelector("#dep_form");

  form.addEventListener("submit", function(e) {
    e.preventDefault();
    var json = {
      time: getTime().text,
      stamp: getTime().stamp,
      date: getDate().dateText,
      data: JSON.parse(toJSONString(this))
    };
    fetch("model.php", {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(json)
    }).then(function(t) {
      t.text().then(function(res) {
        var j = JSON.parse(res);
        console.log(j);
      })
    })

  })

}

The response

Result

5
  • 2
    Try console.log(res) at the bottom there - sounds like it's not valid JSON. Commented Oct 28, 2018 at 7:25
  • Check it now @CertainPerformance Commented Oct 28, 2018 at 7:27
  • 2
    Yep, that's the issue. The {"success":true} should not be at the beginning there - remove whatever's adding it and things might work. Commented Oct 28, 2018 at 7:28
  • Oh, I got the problem, I was sending two responses at one call... Commented Oct 28, 2018 at 7:28
  • 1
    Yes, gotcha .... Thank you mate :) Commented Oct 28, 2018 at 7:28

1 Answer 1

1

Instaed of using .text() and then parsing it, you just can use .json

this.depositForm = function() {
  var form = document.querySelector("#dep_form");

  form.addEventListener("submit", function(e) {
    e.preventDefault();
    var json = {
      time: getTime().text,
      stamp: getTime().stamp,
      date: getDate().dateText,
      data: JSON.parse(toJSONString(this))
    };
    fetch("model.php", {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(json)
    })
    .then((res) => res.json())
    .then((data) => console.log(data))
  })
}

small tip: You should use promise chaining.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.