JavaScript - Parseerror ajax json

 
Vista:

Parseerror ajax json

Publicado por Javier (1 intervención) el 23/01/2019 21:05:00
Buenas tardes, llevo intentando resolver esto bastante tiempo y no consigo quitármelo de encima. Puse una serie de alerts para identificar cual era el error. Es un parseerror, respecto al json. Pero no sé porque, hago las conversiones y sigue dándome el error. Aqui os dejo el archivo php y el javascript a ver si alguien me puede echar una mano.... Muchas gracias.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
require "../clases/Usuario.php";
 
$accion = (isset($_POST["accion"])? $_POST["accion"]:"login");
$nombre = isset($_POST["nombre"]) ? $_POST["nombre"]:'null' ;
$clave = isset($_POST["clave"]) ? $_POST["clave"]:'null'  ;
 
 
$success = false;
$msg = "";
$data = array();
try {
 
    switch ($accion) {
    case 'login':
            $usuario = new Usuario();
            $usuario->nombre = $nombre;
            $usuario->clave = $clave;
            $log = $usuario->login();
            if($log===true){
                $success = true;
                $msg = "Usuario correcto.";
            }else{
                $success = false;
                $msg = "Usuario incorrecto.";
            }
    break;
    default:
        $success=false;
        $msg="Formato no soportado.";
    break;
    }
 
} catch (Exception $e) {
    $success = false;
    $msg = $e->getMessage();
}
 
$response = array(
    "success" => $success,
    "msg" => $msg,
    "data" => $data
);
header('Content-Type: application/json');
echo json_encode($response);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
$( document ).ready(function() {
 
    $("#enviar").click(function(){
        var nombre=$("#nombre").val();
        var clave=$("#clave").val();
        $.ajax({
            type: 'POST',
            url: './servicios/login.php',
            dataType:"application/json",
            data: {"accion": "login",
                "nombre": nombre,
                "clave": clave}
         })
         .done(function (response) {
                // Triggered if response status code is 200 (OK)
 
               if (response.success) {
                  location.href = 'index.html/..';
                } else{
                   alert("Login incorrecto");
                    $("#success_message").html(response.msg);
                }
          })
            .fail(function (jqXHR, status, error) {
                // Triggered if response status code is NOT 200 (OK)
                //alert(jqXHR.responseText);
 
                if (jqXHR.status === 0) {
                    alert('Not connect: Verify Network.');
                  } else if (jqXHR.status === 404) {
                    alert('Requested page not found [404]');
                  } else if (jqXHR.status === 500) {
                    alert('Internal Server Error [500].');
                  } else if (status === 'parsererror') {
                    alert('Requested JSON parse failed.');
                  } else if (status === 'timeout') {
                    alert('Time out error.');
                  } else if (status === 'abort') {
                    alert('Ajax request aborted.');
                  }
            });
    });
});
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.162
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Parseerror ajax json

Publicado por xve (2100 intervenciones) el 24/01/2019 11:44:35
Hola Javier, a simple vista no se ver el error o problema...
Nos puedes comentar exactamente el problema que tienes?

Si haces un
1
console.log(response);
en la linea 16 que te devuelve?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar