I am sending an ajax request and expecting to have a json response. But in my response i am having a blank line and html comments tag ( <!-- --> ). How can i remove this unwanted blank line and characters?

P.S: This happens when i host the website online.

$.ajax({
    url: "edit_row.php",
    method: "post",
    data: {id:id},
    success: function(data){
        alert(data);
    }
});

php:

<?php
    $id = $_POST['id'];
    $data = array();
    -----
    sql query
    ------
    while($query = mysqli_fetch_assoc($sql)){
        $data[] = $query;
    }
    print json_encode($data);
>?

I am expecting the output to be something like this:

[{"id":"1", "asd":"dsa",...}]

But i am getting:


<!-- -->[{"id":"1", "asd":"dsa",...}]

3 Answers

0
Community On

if at all you are sending var id and returning blank then in ajax success call change the code.This is a temporary solution but you should retrospect the code once again in HTML.

$.ajax({
    url: "edit_row.php",
    method: "post",
    data: {id:id},
    success: function(data){
        data=data.replace("<!--","");
        data=data.replace("-->","");
        alert(data);
    }
});
0
SASSY_ROG On

I am not sure what your problem is, but it is important to always wrap your php/code script around a try and catch statement to get php errors and warnings if the is any, in you case e.g

<?php

try {
  $id = $_POST['id'];
  $data = array();
  -----
  sql query
  ------
  while($query = mysqli_fetch_assoc($sql)){
      $data[] = $query;
  }
  print_r(json_encode($data));
}

catch(Exception $e) {
  echo 'Message: ' .$e->getMessage();
}
?>

I also changed you php closing tag from >? to ?> though I'm not sure if that's causing the problem. Also changed print to print_r which is better for readability of arrays and objects.

0
Community On

You have to check all the file which are includes in edit_row.php file and make sure <!-- --> not render.

Still you face the issue, can you provide edit_row.php code.