"parsererror" SyntaxError: Unexpected token in JSON at position 0?

1

I do not stop getting this same error, I have researched about it and it seems that I am sending as html ... something? The concept does not finish to me to be clear in the other answers that have been with the same problem

<?php 
include_once 'header.php';
?>

<script>
      	$(function() {
      		$(document).on('click', '#delete-btn', function (e) {
      			e.preventDefault();

      			var obj = {  
			'id-delete': $('#id-delete').val().trim(),
			'id-delete2': $('#id-delete2').val().trim(),
      			};


      			$.ajax({
      				url: 'back_delete.php',
      				method: 'POST',
      				data: obj,
      				success: function ( respuesta ){
      					alert( respuesta.mensaje );
                                    window.location="index.php";

      				},
      				error: function( e, err, error ){console.log(e, err, error);}
          			//Añadimos un nodo de error, por si pasa algo en el servidor, esto lo vamos a ver en la consola de depuración
        			 
      				
      			})
      		});
      	})
</script>


<div class="main-wrapper"> 
					

			<form class="signup-form"  method="POST"> 
			<h2>Eliminar Cuenta</h2>
			
			<input id="id-delete"type="text" name="id" placeholder="id" required> </input>

			<input id="id-delete2"type="text" name="id2" placeholder="confirme id" required> </input>
			

			<button id="delete-btn" type="submit" name="submit" value="eliminar"> 
			Eliminar
			</button>
			

</form>

<?php 
include_once 'footer.php';
?>

And this is my backend

<?php
session_start();
include_once 'conexion.php';

$repuesta = array();
// la transformo en un array osea que pueda guardar multiples valores en una misma variable



if ( $_REQUEST['#id-delete'] !==  $_REQUEST['#id-delete2'] )
{
	$respuesta['mensaje'] = 'Id incorrecta';
}

else

{

$id_delete = $_REQUEST['id-delete'];

	$sql = "DELETE * FROM Usuario WHERE id = '". $id_delete . "'";
	mysqli_query($conn, $sql);
	// $sql = "UPDATE Usuario SET nick = '". $nicknuevo. "' WHERE id = '".$_SESSION['id'] . "'";

	if (mysqli_affected_rows($conn) > 0) 
	{
		$respuesta['mensaje'] = 'Se elimino con éxito';
	}

	else
	{
		$respuesta['mensaje'] = 'No hubo cambio en la tabla';
	}

}

header('Content-type: application/json');
echo json_encode($respuesta);

?>

EDIT: Fixed Thanks to the best answer that I selected, I checked carefully and I realized that I had if ($ _REQUEST ['# id-delete']! == $ _REQUEST ['# id-delete2']) in my backend but it should be if ($ _REQUEST ['id-delete']! == $ _REQUEST ['id-delete2']) since I can not require the "# id-delete" since I never send a value with that name, but send it as 'id-delete', as you can see in my form 'id-delete': $ ('# id-delete'). val ( ) .trim (), now I no longer have the error (although it still does not delete the table) but the objective of the theme was fulfilled.

EDIT2: Already deleted The following form was not correct

 $sql = "DELETE * FROM Usuario WHERE id = '". $id_delete . "'";

Instead, use this, and it worked.

   $sql = "DELETE FROM Usuario WHERE id = '". $id_delete . "'";
    
asked by Hoozuki 27.08.2018 в 17:08
source

1 answer

1

The problem is that php is sending you an error to see the error placed in the success method of ajax the next thing

success:function(respuesta){
  console.log(respuesta);
}

and be able to fix it from the php file

PS: you can see the error in the browser console

    
answered by 27.08.2018 / 17:40
source