0

I have json parse error, if I request to database. My php code is:

if($result->num_rows){
  header('Content-Type: application/json');
  echo json_encode(array("msg" => "close"));
  $query = "DELETE FROM users WHERE num = '$num' AND password = '$pw'";
  $result = $connection->query($query);
}else {
        $myArr = array("msg" => "Неверный пароль!");
        echo json_encode($myArr);
        exit();
    }

and Ajax request code is:

function ajaxLoad(){
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      var myObj = JSON.parse(this.responseText);
    }
  };
  xmlhttp.open("GET", url, true);
  xmlhttp.send();
}

I tried commenting db request exactly this area

$query = "DELETE FROM users WHERE num = '$num' AND password = '$pw'"; $result = $connection->query($query);

, then my code worked well, but I want to make a request. DB request also working correctly, It deletes the row. I want to say that if i use delete request responseText doesn't work, but it works when i comment the delete request.

Abdy Kerim
  • 11
  • 3
  • Any PHP or console errors? – mplungjan Nov 18 '17 at 12:57
  • If the query is causing this error, then trying to execute it is causing extra output to be generated, making the JSON invalid. When you get the JSON parse error, what is the **exact** value of `this.responseText` (you can `console.log()` or `alert()` it to find out)? Could it be that either `$num` or `$pw` is undefined, and you're getting an "undefined variable" warning from PHP? – rickdenhaan Nov 18 '17 at 12:59
  • No, it is console error, because i have else statement. And it is also working correctly – Abdy Kerim Nov 18 '17 at 13:02
  • Log `this.responseText` before you try to parse it as JSON. You are getting an error because it isn't valid JSON, so figure out what it is! – Quentin Nov 18 '17 at 13:32

1 Answers1

0

It is likely that your database query is failing, there are two parts to fixing this:

  1. You need to fix the error, the first step is to see what the error actually is, you can do this by looking at your logs, or the actual output that is being returned by your AJAX request.

You can do this by viewing it in your browser by just going to the URL that the AJAX request is being made to, or using Dev Tools to view the response (described here: Request Monitoring in Chrome).

Also, you should wrap anything which depends on something external (like a database, or API, or any user data) in a try/catch block, so that you can handle any unexpected errors, something like this:

try {
    $result = $connection->query($query);
}
catch(Exception $e) {
    Log::Error($e->getMessage();
}
  1. Your database error should not be output to screen (or as part of the response in this case).

Turning off displaying errors is done using ini_set('display_errors', 1);, or by actually changing the php.ini, more details can be found here:

Turn off warnings and errors on php/mysql

ThomasRedstone
  • 379
  • 1
  • 4
  • 13